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 edu.ucue.tfc.GUI; import edu.ucue.tfc.Modelo.Video; import edu.ucue.tfc.Service.VideoService; import java.util.Collection; import javax.swing.JOptionPane; import org.jfree.chart.*; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author Paul */ public class GraficarRegistro extends javax.swing.JFrame { /** * Creates new form GraficarRegistro */ public GraficarRegistro() { initComponents(); addItems(); this.setLocationRelativeTo(null); this.GraficoBarras.setVisible(false); } /** * 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() { cbVideos = new javax.swing.JComboBox<>(); GraficoBarras = new javax.swing.JPanel(); btnAceptar = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setLocationByPlatform(true); cbVideos.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Eliga un video..." })); cbVideos.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cbVideosItemStateChanged(evt); } }); GraficoBarras.setBorder(javax.swing.BorderFactory.createEtchedBorder()); javax.swing.GroupLayout GraficoBarrasLayout = new javax.swing.GroupLayout(GraficoBarras); GraficoBarras.setLayout(GraficoBarrasLayout); GraficoBarrasLayout.setHorizontalGroup(GraficoBarrasLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 426, Short.MAX_VALUE)); GraficoBarrasLayout.setVerticalGroup(GraficoBarrasLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 367, Short.MAX_VALUE)); btnAceptar.setText("Aceptar"); btnAceptar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAceptarActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent( cbVideos, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(71, 71, 71).addComponent( GraficoBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(247, 247, 247) .addComponent(btnAceptar))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(cbVideos, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(GraficoBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE) .addComponent(btnAceptar).addGap(30, 30, 30))); pack(); }// </editor-fold>//GEN-END:initComponents private void cbVideosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbVideosItemStateChanged // TODO add your handling code here: if (cbVideos.getSelectedIndex() != 0) { GraficoBarras.removeAll(); GraficoBarras.repaint(); this.GraficoBarras.setVisible(true); int tabla[][] = VideoService.getTabla((((String) cbVideos.getSelectedItem()).split(","))[0]); DefaultCategoryDataset data = new DefaultCategoryDataset(); for (int i = 0; i < 10; i++) { data.addValue(tabla[i][1], "Vehculos", String.valueOf(tabla[i][0])); } ChartPanel panel; JFreeChart chart = null; chart = ChartFactory.createBarChart("Grafico Barras Video", "Segundos", "Cantidad", data, PlotOrientation.HORIZONTAL, true, true, true); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setDomainGridlinesVisible(true); panel = new ChartPanel(chart); panel.setBounds(5, 10, 410, 350); GraficoBarras.add(panel); GraficoBarras.repaint(); } else { JOptionPane.showMessageDialog(rootPane, "No se ha seleccionado un registro!"); return; } }//GEN-LAST:event_cbVideosItemStateChanged private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed // TODO add your handling code here: this.dispose(); }//GEN-LAST:event_btnAceptarActionPerformed /** * @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(GraficarRegistro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GraficarRegistro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(GraficarRegistro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GraficarRegistro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GraficarRegistro().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel GraficoBarras; private javax.swing.JButton btnAceptar; private javax.swing.JComboBox<String> cbVideos; // End of variables declaration//GEN-END:variables private void addItems() { String item; Collection<Video> videos = VideoService.getVideos(); for (Video video : videos) { item = video.getFilePath() + "," + video.getCiudad() + "," + video.getCalle(); cbVideos.addItem(item); } } }