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 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 VentasCiudad extends javax.swing.JFrame { /** * Creates new form Ventas */ Client client = new Client(); public VentasCiudad() { initComponents(); } 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() { jTabbedPane_PI = new javax.swing.JTabbedPane(); jPanel_VentasCiudad = new javax.swing.JPanel(); jTabbedPane3 = new javax.swing.JTabbedPane(); jPanel_ConsultaGeneral = new javax.swing.JPanel(); jButton_executeGeneralVC = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jTable_ConsultaGeneral = new javax.swing.JTable(); jScrollPane9 = new javax.swing.JScrollPane(); jTable_ConsultaSucursal = new javax.swing.JTable(); jButton_executeGeneralVS = new javax.swing.JPanel(); jScrollPane3 = new javax.swing.JScrollPane(); jTable_ConsultaEspecifica = new javax.swing.JTable(); jButton_executeGeneralVC1 = new javax.swing.JButton(); jTextField_Ciudad = new javax.swing.JTextField(); jLabel9 = new javax.swing.JLabel(); jPanel_GraficoVC = new javax.swing.JPanel(); jButtonGrafico = new javax.swing.JButton(); jPanel_GraficoCiudad = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); jButton_executeGeneralVC.setText("Ejecutar"); jButton_executeGeneralVC.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_executeGeneralVCActionPerformed(evt); } }); jTable_ConsultaGeneral.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "ID", "Nombre", "Total Ventas" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jTable_ConsultaGeneral.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTable_ConsultaGeneralMouseClicked(evt); } }); jScrollPane2.setViewportView(jTable_ConsultaGeneral); jTable_ConsultaSucursal.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "ID", "Nombre", "Total Ventas" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jTable_ConsultaSucursal.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTable_ConsultaSucursalMouseClicked(evt); } }); jScrollPane9.setViewportView(jTable_ConsultaSucursal); javax.swing.GroupLayout jPanel_ConsultaGeneralLayout = new javax.swing.GroupLayout(jPanel_ConsultaGeneral); jPanel_ConsultaGeneral.setLayout(jPanel_ConsultaGeneralLayout); jPanel_ConsultaGeneralLayout .setHorizontalGroup( jPanel_ConsultaGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_ConsultaGeneralLayout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane2).addContainerGap()) .addGroup(jPanel_ConsultaGeneralLayout.createSequentialGroup().addGap(236, 236, 236) .addComponent(jButton_executeGeneralVC, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addGap(252, 252, 252)) .addComponent(jScrollPane9)); jPanel_ConsultaGeneralLayout.setVerticalGroup(jPanel_ConsultaGeneralLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_ConsultaGeneralLayout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 160, Short.MAX_VALUE) .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton_executeGeneralVC).addContainerGap())); jTabbedPane3.addTab("Consulta General", jPanel_ConsultaGeneral); jTable_ConsultaEspecifica.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "ID", "Nombre", "Total Ventas" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jScrollPane3.setViewportView(jTable_ConsultaEspecifica); jButton_executeGeneralVC1.setText("Ejecutar"); jButton_executeGeneralVC1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_executeGeneralVC1ActionPerformed(evt); } }); jLabel9.setText("Ciudad:"); javax.swing.GroupLayout jButton_executeGeneralVSLayout = new javax.swing.GroupLayout( jButton_executeGeneralVS); jButton_executeGeneralVS.setLayout(jButton_executeGeneralVSLayout); jButton_executeGeneralVSLayout.setHorizontalGroup(jButton_executeGeneralVSLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jButton_executeGeneralVSLayout.createSequentialGroup() .addGroup(jButton_executeGeneralVSLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jButton_executeGeneralVSLayout.createSequentialGroup() .addGroup(jButton_executeGeneralVSLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jButton_executeGeneralVSLayout.createSequentialGroup() .addGap(148, 148, 148) .addComponent(jButton_executeGeneralVC1)) .addGroup(jButton_executeGeneralVSLayout.createSequentialGroup() .addGap(26, 26, 26).addComponent(jLabel9).addGap(73, 73, 73) .addComponent(jTextField_Ciudad, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 163, Short.MAX_VALUE)) .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap())); jButton_executeGeneralVSLayout.setVerticalGroup( jButton_executeGeneralVSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jButton_executeGeneralVSLayout.createSequentialGroup().addGap(17, 17, 17) .addGroup(jButton_executeGeneralVSLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField_Ciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9)) .addGap(18, 18, 18) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton_executeGeneralVC1).addContainerGap())); jTabbedPane3.addTab("Consulta Detallada", jButton_executeGeneralVS); jButtonGrafico.setText("Graficar"); jButtonGrafico.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonGraficoActionPerformed(evt); } }); javax.swing.GroupLayout jPanel_GraficoCiudadLayout = new javax.swing.GroupLayout(jPanel_GraficoCiudad); jPanel_GraficoCiudad.setLayout(jPanel_GraficoCiudadLayout); jPanel_GraficoCiudadLayout.setHorizontalGroup(jPanel_GraficoCiudadLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); jPanel_GraficoCiudadLayout.setVerticalGroup(jPanel_GraficoCiudadLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 442, Short.MAX_VALUE)); javax.swing.GroupLayout jPanel_GraficoVCLayout = new javax.swing.GroupLayout(jPanel_GraficoVC); jPanel_GraficoVC.setLayout(jPanel_GraficoVCLayout); jPanel_GraficoVCLayout .setHorizontalGroup( jPanel_GraficoVCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_GraficoVCLayout.createSequentialGroup().addGap(334, 334, 334) .addComponent(jButtonGrafico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(156, 156, 156)) .addGroup(jPanel_GraficoVCLayout.createSequentialGroup().addContainerGap() .addComponent(jPanel_GraficoCiudad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); jPanel_GraficoVCLayout.setVerticalGroup( jPanel_GraficoVCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel_GraficoVCLayout.createSequentialGroup().addContainerGap() .addComponent(jPanel_GraficoCiudad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonGrafico, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); jTabbedPane3.addTab("Grafico", jPanel_GraficoVC); javax.swing.GroupLayout jPanel_VentasCiudadLayout = new javax.swing.GroupLayout(jPanel_VentasCiudad); jPanel_VentasCiudad.setLayout(jPanel_VentasCiudadLayout); jPanel_VentasCiudadLayout.setHorizontalGroup(jPanel_VentasCiudadLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane3)); jPanel_VentasCiudadLayout.setVerticalGroup(jPanel_VentasCiudadLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane3)); jTabbedPane_PI.addTab("Ventas Por Ciudad", jPanel_VentasCiudad); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane_PI)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane_PI)); pack(); }// </editor-fold>//GEN-END:initComponents private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing client.cerrarSesion(); }//GEN-LAST:event_formWindowClosing private void jButtonGraficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGraficoActionPerformed jPanel_GraficoCiudad.setLayout(new BorderLayout()); jPanel_GraficoCiudad.add(new ChartPanel( createChart(createDataset(client.consultaGeneralCiudad("0;0", "").split("\\n")), "Total Ventas")), BorderLayout.CENTER); jPanel_GraficoCiudad.validate(); }//GEN-LAST:event_jButtonGraficoActionPerformed private void jButton_executeGeneralVC1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_executeGeneralVC1ActionPerformed //String[] respuesta = client.consultaGeneralCiudad("0;" + jTextField_Ciudad.getText(), "").split(";"); String[] respuesta = client.consultaGeneralCiudad("0;0", jTextField_Ciudad.getText().trim()).split(";"); String[] nombres = { "Id", "Ciudad", "Total Ventas" }; DefaultTableModel dtm = new DefaultTableModel(nombres, respuesta.length); dtm = (DefaultTableModel) this.jTable_ConsultaEspecifica.getModel(); this.jTable_ConsultaGeneral.removeAll(); for (String s : respuesta) { String[] columnasT = s.split("\\t"); dtm.addRow(new Object[] { columnasT[0], columnasT[1], columnasT[2] }); } }//GEN-LAST:event_jButton_executeGeneralVC1ActionPerformed private void jTable_ConsultaSucursalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_ConsultaSucursalMouseClicked // TODO add your handling code here: }//GEN-LAST:event_jTable_ConsultaSucursalMouseClicked private void jTable_ConsultaGeneralMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_ConsultaGeneralMouseClicked this.jTable_ConsultaSucursal.repaint(); String[] nombres = { "Id", "Sucursal", "Total Ventas" }; String[] filasT = client.consultarSucursal("1;" + jTable_ConsultaGeneral.getModel() .getValueAt(jTable_ConsultaGeneral.getSelectionModel().getAnchorSelectionIndex(), 0).toString(), "") .split("\\n"); DefaultTableModel dtm = new DefaultTableModel(nombres, filasT.length); dtm = (DefaultTableModel) this.jTable_ConsultaSucursal.getModel(); for (String s : filasT) { String[] columnasT = s.split("\\t"); dtm.addRow(new Object[] { columnasT[0], columnasT[1], columnasT[2] }); } }//GEN-LAST:event_jTable_ConsultaGeneralMouseClicked private void jButton_executeGeneralVCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_executeGeneralVCActionPerformed String[] nombres = { "Id", "Ciudad", "Total Ventas" }; String[] filasT = client.consultaGeneralCiudad("0;0", "").split("\\n"); DefaultTableModel dtm = new DefaultTableModel(nombres, filasT.length); dtm = (DefaultTableModel) this.jTable_ConsultaGeneral.getModel(); this.jTable_ConsultaGeneral.removeAll(); for (String s : filasT) { String[] columnasT = s.split("\\t"); dtm.addRow(new Object[] { columnasT[0], columnasT[1], columnasT[2] }); } }//GEN-LAST:event_jButton_executeGeneralVCActionPerformed /** * @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(VentasCiudad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(VentasCiudad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(VentasCiudad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(VentasCiudad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(() -> { new VentasCiudad().setVisible(true); }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonGrafico; private javax.swing.JButton jButton_executeGeneralVC; private javax.swing.JButton jButton_executeGeneralVC1; private javax.swing.JPanel jButton_executeGeneralVS; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel_ConsultaGeneral; private javax.swing.JPanel jPanel_GraficoCiudad; private javax.swing.JPanel jPanel_GraficoVC; private javax.swing.JPanel jPanel_VentasCiudad; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane9; private javax.swing.JTabbedPane jTabbedPane3; private javax.swing.JTabbedPane jTabbedPane_PI; private javax.swing.JTable jTable_ConsultaEspecifica; private javax.swing.JTable jTable_ConsultaGeneral; private javax.swing.JTable jTable_ConsultaSucursal; private javax.swing.JTextField jTextField_Ciudad; // End of variables declaration//GEN-END:variables }