Java tutorial
package view.caja; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.StringTokenizer; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSpinner; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.SpinnerListModel; import javax.swing.SpinnerNumberModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import org.jfree.chart.axis.AxisState; import util.NumeroALetras; import view.admin.AdminUI; import controller.cliente.ClienteController; import controller.producto.ProductoController; import controller.venta.VentaController; import com.mxrck.autocompleter.TextAutoCompleter; import model.FactoryBeans.FactoryDetalleVenta; import model.FactoryBeans.FactoryFactura; import model.FactoryBeans.FactoryProducto; import model.FactoryBeans.FactoryVenta; import model.beans.Categoria; import model.beans.Cliente; import model.beans.Factura; import model.beans.Producto; import model.beans.Usuario; import model.beans.Venta; /** * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI * Builder, which is free for non-commercial use. If Jigloo is being used * commercially (ie, by a corporation, company or business for any purpose * whatever) then you should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. Use of Jigloo implies * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR * ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class Despacho extends JFrame { { //Set Look & Feel try { javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } } private static final long serialVersionUID = 1L; private JPanel panRoot; private JLabel lbTitulo; private JLabel lbTituloForm; private JSpinner jspn; private JLabel jlblPagado; private JLabel jlblACobrarMostrar; private JLabel jlblACobrar; private JLabel jlblCantMostrar; private JLabel jlblCantidad; private JLabel jlblNit; private JTextField jtxtnit; private JTextField jtxtCampo; private JLabel jlblNombre; private JLabel jlblDatosCliente; private JButton jbtnCancelar; private JButton jbtnConfirmar; private JLabel jlblQuitarTodos; private JLabel jlblQuitar; private JLabel jlblAniadir; private JButton btAdd; private JLabel lbTituloCodigo; private JTable tbCarrito; private JScrollPane scrollProds; private Usuario usuario; private JButton btQuitar; private int fila, columna = -1; private JButton btRemoveAll; private int contador = 0; private ProductoController productoController; private Producto p; private int cant; private double aCobrar; private ClienteController clienteController; private ArrayList<Cliente> cl; private Cliente comprador; private double cambio; public double getaCobrar() { return aCobrar; } public void setaCobrar(double aCobrar) { this.aCobrar = aCobrar; } public int getCant() { return cant; } public void setCant(int cant) { if (cant > 0) this.cant = cant; } public Despacho(Usuario usuario) { productoController = new ProductoController(); clienteController = new ClienteController(); cl = clienteController.mostrarClientes(); aCobrar = 0; this.usuario = usuario; cambio = 0; initGUI(); //cl = clienteController.get addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); pack(); } private void initGUI() { try { { panRoot = new JPanel(); getContentPane().add(panRoot, BorderLayout.CENTER); panRoot.setLayout(null); panRoot.setPreferredSize(new java.awt.Dimension(810, 498)); { scrollProds = getTablaProductos(); panRoot.add(scrollProds); scrollProds.setBounds(15, 106, 390, 287); } { lbTitulo = new JLabel(); panRoot.add(lbTitulo); lbTitulo.setText("LISTA DE PRODUCTOS :"); lbTitulo.setFont(new Font("Helvetica", Font.BOLD, 15)); lbTitulo.setBounds(15, 76, 243, 16); } { lbTituloCodigo = new JLabel(); panRoot.add(lbTituloCodigo); lbTituloCodigo.setText("Agregando Productos"); lbTituloCodigo.setFont(new Font("Helvetica", Font.BOLD, 15)); lbTituloCodigo.setBounds(572, 179, 168, 16); } { btAdd = new JButton(); panRoot.add(btAdd); btAdd.setBounds(529, 220, 57, 45); btAdd.setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/add.png"))); btAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { agregarNuevoProductoParaVender(); } }); } { btQuitar = new JButton(); panRoot.add(btQuitar); btQuitar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/delete.png"))); btQuitar.setBounds(529, 270, 57, 48); btQuitar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { quitarFila(); } }); btQuitar.setEnabled(false); } { btRemoveAll = new JButton(); panRoot.add(btRemoveAll); btRemoveAll.setBounds(532, 324, 54, 54); btRemoveAll .setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/deleteAll.png"))); btRemoveAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { quitarTodo(); } }); btRemoveAll.setEnabled(false); } { lbTituloForm = new JLabel(); panRoot.add(lbTituloForm); lbTituloForm.setText("REALIZANDO VENTA"); lbTituloForm.setBounds(241, 20, 253, 44); lbTituloForm.setFont(new Font("Monospaced", Font.BOLD, 25)); } { jlblAniadir = new JLabel(); panRoot.add(jlblAniadir); jlblAniadir.setText("Aadir"); jlblAniadir.setFont(new Font("Helvetica", Font.BOLD, 13)); jlblAniadir.setBounds(592, 230, 68, 23); } { jlblQuitar = new JLabel(); panRoot.add(jlblQuitar); jlblQuitar.setText("Quitar"); jlblQuitar.setFont(new Font("Helvetica", Font.BOLD, 13)); jlblQuitar.setBounds(592, 281, 68, 16); } { jlblQuitarTodos = new JLabel(); panRoot.add(jlblQuitarTodos); jlblQuitarTodos.setText("Quitar Todos"); jlblQuitarTodos.setFont(new Font("Helvetica", Font.BOLD, 13)); jlblQuitarTodos.setBounds(592, 338, 88, 16); } { jbtnConfirmar = new JButton(); panRoot.add(jbtnConfirmar); jbtnConfirmar.setText(" Confirmar"); jbtnConfirmar .setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/acepted3.png"))); jbtnConfirmar.setBounds(85, 412, 167, 62); jbtnConfirmar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (contador != 0 && jtxtCampo.getText() != "" && jtxtnit.getText() != "") { cambio = aCobrar - Double.parseDouble(jspn.getValue().toString()); efectuar(); JOptionPane.showMessageDialog(null, "Se debe dar el cambio de " + cambio, "Alerta", 2); } else { JOptionPane.showMessageDialog(null, "Existen Campos Obligatorios para la venta", "Alerta", 2); } } public void efectuar() { FactoryFactura factFactura = new FactoryFactura(); int idFactura = 0; int idCliente = comprador.getId_Cliente(); int idUsuario = usuario.getIdusuario(); Factura fact = new Factura(); fact.setNit(comprador.getNit()); fact.setTotal(Float.parseFloat(Double.toString(aCobrar))); fact.setCambio(Float.parseFloat(cambio + "")); try { idFactura = factFactura.insertFactura(fact); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } FactoryVenta factVenta = new FactoryVenta(); Venta nuevaVenta = new Venta(); nuevaVenta.setFechaVenta(factVenta.getFechaHoy()); nuevaVenta.setHventa(factVenta.getHora()); nuevaVenta.setId_Cliente(idCliente); nuevaVenta.setId_factura(idFactura); nuevaVenta.setId_Usario(idUsuario); try { if (factVenta.insertVenta(nuevaVenta)) { } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } FactoryDetalleVenta factDVenta = new FactoryDetalleVenta(); for (int i = 0; i < tbCarrito.getRowCount(); i++) { int idprod = (Integer) tbCarrito.getValueAt(i, 5); int idventaMax = factVenta.getIdMaximo(); int cantidad = (Integer) tbCarrito.getValueAt(i, 4); try { factDVenta.insertDetalleVenta(idprod, idventaMax, cantidad); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } setVisible(false); JOptionPane.showMessageDialog(null, "La Venta se ha realizado exitosamente...", "Felicidades", 1); } }); } { jbtnCancelar = new JButton(); panRoot.add(jbtnCancelar); jbtnCancelar.setText(" Cancelar"); jbtnCancelar .setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/cancelShop.png"))); jbtnCancelar.setBounds(294, 413, 170, 60); jbtnCancelar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); } { jlblDatosCliente = new JLabel(); panRoot.add(jlblDatosCliente); jlblDatosCliente.setText("Datos de Cliente"); jlblDatosCliente.setFont(new Font("Helvetica", Font.BOLD, 15)); jlblDatosCliente.setBounds(580, 64, 126, 16); } { jlblNombre = new JLabel(); panRoot.add(jlblNombre); jlblNombre.setText("Nombre : "); jlblNombre.setBounds(545, 138, 69, 18); } { jtxtCampo = new JTextField(); panRoot.add(jtxtCampo); jtxtCampo.setBounds(603, 133, 137, 28); } { jtxtnit = new JTextField(); panRoot.add(jtxtnit); jtxtnit.setBounds(602, 92, 140, 28); jtxtnit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String cad = jtxtnit.getText(); comprador = clienteABuscar(cl, cad); jtxtCampo.setText(comprador.getNombreCliente()); jtxtCampo.setEditable(false); } }); TextAutoCompleter autoCompl = new TextAutoCompleter(jtxtnit); for (int i = 0; i < cl.size(); i++) { Cliente c = cl.get(i); autoCompl.addItem(c.getNit()); } autoCompl.setMode(0); } { jlblNit = new JLabel(); panRoot.add(jlblNit); jlblNit.setText("Nit o CI :"); jlblNit.setBounds(542, 99, 46, 16); } { jlblCantidad = new JLabel(); panRoot.add(jlblCantidad); jlblCantidad.setText("Cantidad :"); jlblCantidad.setFont(new Font("Helvetica", Font.BOLD, 15)); jlblCantidad.setBounds(424, 169, 79, 16); } { jlblCantMostrar = new JLabel(); panRoot.add(jlblCantMostrar); jlblCantMostrar.setBounds(435, 183, 68, 58); jlblCantMostrar.setText("0"); jlblCantMostrar.setFont(new Font("Monospaced", Font.BOLD, 35)); } { jlblACobrar = new JLabel(); panRoot.add(jlblACobrar); jlblACobrar.setText("A Cobrar :"); jlblACobrar.setFont(new Font("Helvetica", Font.BOLD, 15)); jlblACobrar.setBounds(424, 270, 79, 16); } { jlblACobrarMostrar = new JLabel(); panRoot.add(jlblACobrarMostrar); jlblACobrarMostrar.setBounds(430, 292, 68, 58); jlblACobrarMostrar.setText("0.0"); jlblACobrarMostrar.setFont(new Font("Monospaced", Font.BOLD, 29)); } { jlblPagado = new JLabel(); panRoot.add(jlblPagado); jlblPagado.setText("Monto con el que se pago Bs :"); jlblPagado.setBounds(532, 420, 168, 16); } { SpinnerNumberModel jSpinnerCantidadModel = new SpinnerNumberModel(0, 0, 1000, 1); jspn = new JSpinner(); panRoot.add(jspn); jspn.setModel(jSpinnerCantidadModel); jspn.setBounds(706, 414, 59, 28); } } pack(); } catch (Exception e) { e.printStackTrace(); } } private Cliente clienteABuscar(ArrayList<Cliente> cl, String cliSearch) { Cliente res = null; for (int i = 0; i < cl.size(); i++) { if (cl.get(i).getNit().equals(cliSearch)) { res = cl.get(i); break; } } return res; } protected void agregarNuevoProductoParaVender() { productoController.agregarNuevoProductoParaVender(this); agregarFila(); } protected void agregarProductoActionPerformed() { productoController.agregarNuevoProductoParaVender(this); } public JScrollPane getTablaProductos() { Object[][] datitos = {}; String[] columnas_Name = { "#", "Producto", "Precio", "Codigo", "Cantidad", "IDprod" }; TableModel jTable1Model = new DefaultTableModel(datitos, columnas_Name) { public boolean isCellEditable(int row, int column) { return false; } }; tbCarrito = new JTable(); tbCarrito.setModel(jTable1Model); tbCarrito.getTableHeader().setReorderingAllowed(false); tbCarrito.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); tbCarrito.setPreferredScrollableViewportSize(new Dimension(400, 350)); tbCarrito.setPreferredSize(new java.awt.Dimension(351, 283)); tbCarrito.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); tbCarrito.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (tbCarrito.getRowCount() != 0) { fila = tbCarrito.getSelectedRow(); columna = tbCarrito.getSelectedColumn(); btQuitar.setEnabled(true); } } }); scrollProds = new JScrollPane(tbCarrito); return scrollProds; } public void agregarFila() { if (p != null) { // { "#", "Producto","Precio","Codigo","Cantidad"} DefaultTableModel defModel = (DefaultTableModel) tbCarrito.getModel(); contador++; defModel.addRow( new Object[] { contador, p.getNomprod(), p.getPventa(), p.getCodprod(), cant, p.getIdprod() }); aCobrar = aCobrar + (p.getPventa() * cant); jlblACobrarMostrar.setText(Double.toString(aCobrar)); jlblCantMostrar.setText(Integer.toString(contador)); //System.out.println(aCobrar); btRemoveAll.setEnabled(true); } } public void quitarFila() { if (tbCarrito.getRowCount() != 0) { if (fila >= 0) { contador--; DefaultTableModel defModel = (DefaultTableModel) tbCarrito.getModel(); //System.out.println(Double.parseDouble(tbCarrito.getValueAt(fila,2).toString())); //System.out.println(Integer.parseInt(tbCarrito.getValueAt(fila,4).toString())); double valProdSelected = Double.parseDouble(tbCarrito.getValueAt(fila, 2).toString()) * Integer.parseInt(tbCarrito.getValueAt(fila, 4).toString()); aCobrar = aCobrar - valProdSelected; jlblACobrarMostrar.setText(Double.toString(aCobrar)); jlblCantMostrar.setText(Integer.toString(contador)); defModel.removeRow(fila); fila = -1; for (int indexTable = tbCarrito.getRowCount() - 1; indexTable >= 0; indexTable--) { defModel.setValueAt(indexTable + 1, indexTable, 0); } System.out.println(aCobrar); btQuitar.setEnabled(false); if (tbCarrito.getRowCount() == 0) { btRemoveAll.setEnabled(false); } } } else { btQuitar.setEnabled(false); System.out.println("nada que quitar"); } } public void quitarTodo() { //System.out.println("#Items:"+tbCarrito.getRowCount()); if (tbCarrito.getRowCount() != 0) { DefaultTableModel defModel = (DefaultTableModel) tbCarrito.getModel(); for (int indexTable = tbCarrito.getRowCount() - 1; indexTable >= 0; indexTable--) { defModel.removeRow(indexTable); } contador = 0; jlblCantMostrar.setText(Integer.toString(contador)); aCobrar = 0; jlblACobrarMostrar.setText(Double.toString(aCobrar)); btRemoveAll.setEnabled(false); if (tbCarrito.getRowCount() == 0) { btQuitar.setEnabled(false); } aCobrar = 0; jlblACobrarMostrar.setText(Double.toString(aCobrar)); System.out.println(aCobrar); } else { //System.out.println("nada que quitar"); btRemoveAll.setEnabled(false); } } public Producto getP() { return p; } public void setP(Producto p) { if (p != null) this.p = p; } }