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 flight_ranker; import java.awt.Color; import java.util.Locale; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PiePlot3D; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; /** * * @author Aniket */ public class my_Jframe extends javax.swing.JFrame { /** * Creates new form my_Jframe */ public my_Jframe() { initComponents(); } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton2 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { { new Integer(111), new Integer(222), new Integer(333), new Integer(444), new Integer(555) }, { new Integer(122), new Integer(222), new Integer(333), new Integer(444), new Integer(555) }, { new Integer(111), new Integer(222), new Integer(333), new Integer(444), new Integer(555) }, { new Integer(111), new Integer(333), new Integer(222), new Integer(444), new Integer(555) } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4", "Title5" }) { Class[] types = new Class[] { java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jScrollPane1.setViewportView(jTable1); jButton2.setText("Pie chart"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton1.setText("Pie Chart3D"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton3.setText("Bar Chart"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(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().addContainerGap(290, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jButton1).addGap(55, 55, 55) .addComponent(jButton2).addGap(64, 64, 64).addComponent(jButton3) .addGap(55, 55, 55))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(38, 38, 38) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(41, 41, 41) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2).addComponent(jButton1).addComponent(jButton3)) .addContainerGap(279, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed DefaultPieDataset piedataset = new DefaultPieDataset(); piedataset.setValue("One", new Integer(10)); piedataset.setValue("Two", new Integer(20)); piedataset.setValue("Three", new Integer(30)); piedataset.setValue("Four", new Integer(40)); JFreeChart chart = ChartFactory.createPieChart("Pie chart", piedataset, true, true, true); PiePlot P = (PiePlot) chart.getPlot(); //P.setForegroundAlpha(TOP_ALIGNMENT); ChartFrame frame = new ChartFrame("Pie chart", chart); frame.setVisible(true); frame.setSize(450, 500); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed DefaultPieDataset piedataset = new DefaultPieDataset(); piedataset.setValue("One", new Integer(10)); piedataset.setValue("Two", new Integer(20)); piedataset.setValue("Three", new Integer(30)); piedataset.setValue("Four", new Integer(40)); JFreeChart chart = ChartFactory.createPieChart3D("Pie chart", piedataset, true, true, true); PiePlot3D P = (PiePlot3D) chart.getPlot(); // P.setForegroundAlpha(TOP_ALIGNMENT); ChartFrame frame = new ChartFrame("Pie chart", chart); frame.setVisible(true); frame.setSize(450, 500); }//GEN-LAST:event_jButton1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(85, "Marks", "Science"); dataset.setValue(91, "Marks", "Maths"); dataset.setValue(97, "Marks", "Computer"); dataset.setValue(95, "Marks", "English"); JFreeChart chart = ChartFactory.createBarChart("Bar Chart", "Subject", "Marks", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot P = chart.getCategoryPlot(); P.setRangeGridlinePaint(Color.black); ChartFrame frame = new ChartFrame("Bar chart", chart); frame.setVisible(true); frame.setSize(450, 351); }//GEN-LAST:event_jButton3ActionPerformed /** * @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(my_Jframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(my_Jframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(my_Jframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(my_Jframe.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 my_Jframe().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }