Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Vista; import Modelo.Client; import java.awt.BorderLayout; import java.awt.HeadlessException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot3D; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; import org.jfree.util.Rotation; /** * * @author alexander */ public class CuentasCobrar extends javax.swing.JFrame { /** * Creates new form CuentasCobrar */ public CuentasCobrar() { initComponents(); insertarCB(jComboBox_Facturas, client.cargaFactura()); } private void insertarCB(JComboBox seleccion, ArrayList<String> lista) { lista.stream().forEach((s) -> { if (s.contains(";")) { String[] temp = s.split(";"); seleccion.addItem(temp[temp.length - 1]); } else { seleccion.addItem(s); } }); } Client client = new Client(); private PieDataset createDataset(String[] filasT) { DefaultPieDataset pieDataset = new DefaultPieDataset(); // String[] filasT = client.consultaGeneralCiudad("0;0").split("\\n"); for (String s : filasT) { String[] columnaT = s.split("\\t"); pieDataset.setValue(columnaT[1], Double.parseDouble(columnaT[2])); } return pieDataset; } private PieDataset createDataset2(String[] filasT) { DefaultPieDataset pieDataset = new DefaultPieDataset(); // String[] filasT = client.consultaGeneralCiudad("0;0").split("\\n"); for (String s : filasT) { String[] columnaT = s.split("\\t"); pieDataset.setValue(columnaT[1], Double.parseDouble(columnaT[5])); } return pieDataset; } private JFreeChart createChart(PieDataset dataset, String title) { JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title dataset, // data true, // include legend true, false); //chart.getLegend(); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); return chart; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jTabbedPane5 = new javax.swing.JTabbedPane(); jPanel_InsertarAbono = new javax.swing.JPanel(); jTextField_Abono = new javax.swing.JTextField(); jComboBox_Facturas = new javax.swing.JComboBox(); jLabel11 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel(); jButton_InsertarAbono = new javax.swing.JButton(); jPanel_ConsultaAbono = new javax.swing.JPanel(); jScrollPane6 = new javax.swing.JScrollPane(); jTable_CuentasPorCobrar = new javax.swing.JTable(); jButton_EjecuteCXC = new javax.swing.JButton(); jPanel_GraficoAbono = new javax.swing.JPanel(); jPanel_GraficoCuentasPorCobrar = new javax.swing.JPanel(); jButton_GraficoCuentaPorCobrar = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jScrollPane7 = new javax.swing.JScrollPane(); jTable_CuentasPorCobrar1 = new javax.swing.JTable(); jButton_EjecuteCXC1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jTextField_Ciudad = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); jTextField_Abono.setText("0"); jLabel11.setText("Abono:"); jLabel12.setText("Factura:"); jButton_InsertarAbono.setText("Insertar"); jButton_InsertarAbono.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_InsertarAbonoActionPerformed(evt); } }); javax.swing.GroupLayout jPanel_InsertarAbonoLayout = new javax.swing.GroupLayout(jPanel_InsertarAbono); jPanel_InsertarAbono.setLayout(jPanel_InsertarAbonoLayout); jPanel_InsertarAbonoLayout.setHorizontalGroup(jPanel_InsertarAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_InsertarAbonoLayout.createSequentialGroup().addContainerGap() .addGroup(jPanel_InsertarAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel11).addComponent(jLabel12)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel_InsertarAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField_Abono) .addComponent(jComboBox_Facturas, 0, 221, Short.MAX_VALUE)) .addGap(35, 35, 35)) .addGroup(jPanel_InsertarAbonoLayout.createSequentialGroup().addGap(218, 218, 218) .addComponent(jButton_InsertarAbono).addContainerGap(106, Short.MAX_VALUE))); jPanel_InsertarAbonoLayout.setVerticalGroup(jPanel_InsertarAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_InsertarAbonoLayout.createSequentialGroup().addGap(41, 41, 41) .addGroup(jPanel_InsertarAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField_Abono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel11)) .addGap(48, 48, 48) .addGroup(jPanel_InsertarAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jComboBox_Facturas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel12)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 193, Short.MAX_VALUE) .addComponent(jButton_InsertarAbono).addContainerGap())); jTabbedPane5.addTab("Insertar", jPanel_InsertarAbono); jTable_CuentasPorCobrar.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Nombre", "Numero Contacto", "Empresa", "Precio Total Facturas" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jScrollPane6.setViewportView(jTable_CuentasPorCobrar); jButton_EjecuteCXC.setText("Ejecutar"); jButton_EjecuteCXC.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_EjecuteCXCActionPerformed(evt); } }); javax.swing.GroupLayout jPanel_ConsultaAbonoLayout = new javax.swing.GroupLayout(jPanel_ConsultaAbono); jPanel_ConsultaAbono.setLayout(jPanel_ConsultaAbonoLayout); jPanel_ConsultaAbonoLayout.setHorizontalGroup( jPanel_ConsultaAbonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_ConsultaAbonoLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton_EjecuteCXC).addContainerGap())); jPanel_ConsultaAbonoLayout.setVerticalGroup(jPanel_ConsultaAbonoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_ConsultaAbonoLayout.createSequentialGroup().addContainerGap(39, Short.MAX_VALUE) .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton_EjecuteCXC).addGap(13, 13, 13))); jTabbedPane5.addTab("Consulta General", jPanel_ConsultaAbono); javax.swing.GroupLayout jPanel_GraficoCuentasPorCobrarLayout = new javax.swing.GroupLayout( jPanel_GraficoCuentasPorCobrar); jPanel_GraficoCuentasPorCobrar.setLayout(jPanel_GraficoCuentasPorCobrarLayout); jPanel_GraficoCuentasPorCobrarLayout.setHorizontalGroup(jPanel_GraficoCuentasPorCobrarLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 375, Short.MAX_VALUE)); jPanel_GraficoCuentasPorCobrarLayout.setVerticalGroup(jPanel_GraficoCuentasPorCobrarLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 293, Short.MAX_VALUE)); jButton_GraficoCuentaPorCobrar.setText("Graficar"); jButton_GraficoCuentaPorCobrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_GraficoCuentaPorCobrarActionPerformed(evt); } }); javax.swing.GroupLayout jPanel_GraficoAbonoLayout = new javax.swing.GroupLayout(jPanel_GraficoAbono); jPanel_GraficoAbono.setLayout(jPanel_GraficoAbonoLayout); jPanel_GraficoAbonoLayout.setHorizontalGroup( jPanel_GraficoAbonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_GraficoAbonoLayout.createSequentialGroup().addContainerGap() .addComponent(jPanel_GraficoCuentasPorCobrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_GraficoAbonoLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton_GraficoCuentaPorCobrar).addGap(140, 140, 140))); jPanel_GraficoAbonoLayout.setVerticalGroup( jPanel_GraficoAbonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_GraficoAbonoLayout.createSequentialGroup().addContainerGap() .addComponent(jPanel_GraficoCuentasPorCobrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(18, 18, 18).addComponent(jButton_GraficoCuentaPorCobrar) .addContainerGap())); jTabbedPane5.addTab("Grafico", jPanel_GraficoAbono); jTable_CuentasPorCobrar1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Nombre", "Numero Contacto", "Empresa", "Precio Total Facturas" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jScrollPane7.setViewportView(jTable_CuentasPorCobrar1); jButton_EjecuteCXC1.setText("Ejecutar"); jButton_EjecuteCXC1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_EjecuteCXC1ActionPerformed(evt); } }); jLabel1.setText("Ciudad:"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton_EjecuteCXC1).addContainerGap()) .addGroup(jPanel1Layout.createSequentialGroup().addGap(71, 71, 71).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextField_Ciudad, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(42, 42, 42))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap(13, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jTextField_Ciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton_EjecuteCXC1).addGap(13, 13, 13))); jTabbedPane5.addTab("Ciudad", jPanel1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane5)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane5)); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton_InsertarAbonoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_InsertarAbonoActionPerformed try { client.insertarAbono(jComboBox_Facturas.getSelectedItem().toString() + ";" + jTextField_Abono.getText() + ";" + new SimpleDateFormat("yyyy-MM-dd").format(new Date())); } catch (HeadlessException ex) { JOptionPane.showMessageDialog(null, ex); } }//GEN-LAST:event_jButton_InsertarAbonoActionPerformed private void jButton_EjecuteCXCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_EjecuteCXCActionPerformed // TODO add your handling code here: String[] resp = client.consultarCuentas("0;0", Client.getCity()).split("\\n"); String[] nombres = { "Nombre", "Numero de Contacto", "Empresa", "Numero de facturas" }; DefaultTableModel dtm = new DefaultTableModel(nombres, resp.length); dtm = (DefaultTableModel) this.jTable_CuentasPorCobrar.getModel(); this.jTable_CuentasPorCobrar.removeAll(); for (String s : resp) { String[] columnasT = s.split("\\t"); dtm.addRow(new Object[] { columnasT[1], columnasT[2], columnasT[3], columnasT[4] }); } }//GEN-LAST:event_jButton_EjecuteCXCActionPerformed private void jButton_GraficoCuentaPorCobrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_GraficoCuentaPorCobrarActionPerformed // TODO add your handling code here: try { jPanel_GraficoCuentasPorCobrar.setLayout(new BorderLayout()); jPanel_GraficoCuentasPorCobrar.add(new ChartPanel(createChart( createDataset2(client.consultarCuentas("0;0", Client.getCity()).split("\\n")), "Clientes")), BorderLayout.CENTER); jPanel_GraficoCuentasPorCobrar.validate(); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex); } }//GEN-LAST:event_jButton_GraficoCuentaPorCobrarActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing // TODO add your handling code here: client.cerrarSesion(); }//GEN-LAST:event_formWindowClosing private void jButton_EjecuteCXC1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_EjecuteCXC1ActionPerformed // TODO add your handling code here: String[] resp = client.consultarCuentas("0;0", jTextField_Ciudad.getText().trim()).split("\\n"); String[] nombres = { "Nombre", "Numero de Contacto", "Empresa", "Numero de facturas" }; DefaultTableModel dtm = new DefaultTableModel(nombres, resp.length); dtm = (DefaultTableModel) this.jTable_CuentasPorCobrar.getModel(); this.jTable_CuentasPorCobrar.removeAll(); for (String s : resp) { String[] columnasT = s.split("\\t"); dtm.addRow(new Object[] { columnasT[1], columnasT[2], columnasT[3], columnasT[4] }); } }//GEN-LAST:event_jButton_EjecuteCXC1ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(CuentasCobrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CuentasCobrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CuentasCobrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CuentasCobrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(() -> { new CuentasCobrar().setVisible(true); }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton_EjecuteCXC; private javax.swing.JButton jButton_EjecuteCXC1; private javax.swing.JButton jButton_GraficoCuentaPorCobrar; private javax.swing.JButton jButton_InsertarAbono; private javax.swing.JComboBox jComboBox_Facturas; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel_ConsultaAbono; private javax.swing.JPanel jPanel_GraficoAbono; private javax.swing.JPanel jPanel_GraficoCuentasPorCobrar; private javax.swing.JPanel jPanel_InsertarAbono; private javax.swing.JScrollPane jScrollPane6; private javax.swing.JScrollPane jScrollPane7; private javax.swing.JTabbedPane jTabbedPane5; private javax.swing.JTable jTable_CuentasPorCobrar; private javax.swing.JTable jTable_CuentasPorCobrar1; private javax.swing.JTextField jTextField_Abono; private javax.swing.JTextField jTextField_Ciudad; // End of variables declaration//GEN-END:variables }