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 tusys.view; import java.awt.Color; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import tusys.database.*; /** * * @author Candy Olivia Mawalim */ public class jPanelStatistik extends javax.swing.JPanel { private Statistic stat = new Statistic(); private MainMenu mainMenu; private java.sql.Date tanggal_mulai; private java.sql.Date tanggal_selesai; private String jenis_kegiatan; public MainMenu getMainMenu() { return mainMenu; } public void setMainMenu(MainMenu mainMenu) { this.mainMenu = mainMenu; } /** * Creates new form jPanelStatistik */ public jPanelStatistik() { initComponents(); jPanel1 = new jPanelChart("Statistik Penggunaan Ruangan"); jPanel1.setVisible(true); } /** * 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() { jPanel2 = new javax.swing.JPanel(); jTextFieldTanggalSelesai = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox(); jTextFieldTanggalMulai = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); setLayout(new java.awt.BorderLayout()); jTextFieldTanggalSelesai.setToolTipText(""); jTextFieldTanggalSelesai.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextFieldTanggalSelesaiActionPerformed(evt); } }); jLabel3.setText("Tanggal Selesai"); jPanel1.setBackground(new java.awt.Color(102, 102, 255)); jPanel1.setLayout(new java.awt.BorderLayout()); jButton1.setText("Show Statistic"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); jLabel2.setText("Jenis Kegiatan"); jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP); jLabel2.setPreferredSize(new java.awt.Dimension(50, 14)); jLabel1.setText("Tanggal Mulai"); jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "Kuliah", "Seminar", "Himpunan", "Unit", "Semua" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel4.setForeground(new java.awt.Color(0, 0, 255)); jLabel4.setText("Format YYYY-MM-DD"); jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel5.setForeground(new java.awt.Color(0, 0, 255)); jLabel5.setText("Format YYYY-MM-DD"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(18, 18, 18).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 554, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jTextFieldTanggalMulai, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextFieldTanggalSelesai, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 572, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(19, 19, 19).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldTanggalMulai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextFieldTanggalSelesai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(15, 15, 15))) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1).addContainerGap())); add(jPanel2, java.awt.BorderLayout.LINE_END); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed jenis_kegiatan = jComboBox1.getSelectedItem().toString(); System.out.println(jenis_kegiatan); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { tanggal_mulai = new java.sql.Date(sdf.parse(jTextFieldTanggalMulai.getText()).getTime()); tanggal_selesai = new java.sql.Date(sdf.parse(jTextFieldTanggalSelesai.getText()).getTime()); if (tanggal_mulai.after(tanggal_selesai)) { JOptionPane.showMessageDialog(null, "tanggal mulai harus sebelum tanggal selesai"); return; } } catch (ParseException ex) { JOptionPane.showMessageDialog(null, "Format tanggal masukan salah"); return; //Logger.getLogger(jPanelStatistik.class.getName()).log(Level.SEVERE, null, ex); } if (jenis_kegiatan.equals("Semua")) { try { stat = mainMenu.getDbc().getAllStatistic(tanggal_mulai, tanggal_selesai); } catch (SQLException ex) { Logger.getLogger(jPanelStatistik.class.getName()).log(Level.SEVERE, null, ex); } } else { try { stat = mainMenu.getDbc().getStatistic(jenis_kegiatan, (tanggal_mulai), (tanggal_selesai)); } catch (SQLException ex) { Logger.getLogger(jPanelStatistik.class.getName()).log(Level.SEVERE, null, ex); } } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < stat.getRuangan().size(); i++) { dataset.setValue(stat.getFrekuensi().get(i), "Frekuensi", stat.getRuangan().get(i)); } JFreeChart chart = ChartFactory.createBarChart("Statistik Penggunaan Ruangan", "Ruangan", "Frekuensi", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot p = chart.getCategoryPlot(); p.setRangeGridlinePaint(Color.BLACK); ValueAxis yAxis = chart.getCategoryPlot().getRangeAxis(); yAxis.setRange(0.0, 100.0); p.setRangeAxis(yAxis); ChartFrame frame = new ChartFrame("Statistik", chart); frame.setVisible(true); frame.setLocation(500, 200); frame.setSize(565, 410); /* //masih ga keluar gambarnya ChartPanel cp = new ChartPanel(chart); jPanel1.removeAll(); jPanel1.add(cp,BorderLayout.CENTER); jPanel1.revalidate(); mainMenu.getjPanelData1().add(jPanel1); */ }//GEN-LAST:event_jButton1ActionPerformed private void jTextFieldTanggalSelesaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTanggalSelesaiActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextFieldTanggalSelesaiActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jComboBox1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JTextField jTextFieldTanggalMulai; private javax.swing.JTextField jTextFieldTanggalSelesai; // End of variables declaration//GEN-END:variables }