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 user; import java.awt.Image; import java.awt.event.WindowEvent; import javax.swing.DefaultComboBoxModel; import javax.swing.Icon; import javax.swing.ImageIcon; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; /** * * @author Nisha */ public class Screen extends javax.swing.JPanel { /** * Creates new form NewJPanel2 */ public Screen(Database database) { this.database = database; query = new Query(database); initComponents(); setCheckbox(); showChart(); } public static void setChartLabel(ImageIcon ii) { chartLabel.setIcon(ii); } private void setCheckBox(String[] items) { DefaultComboBoxModel model = (DefaultComboBoxModel) choiceBox2.getModel(); model.removeAllElements(); for (String item : items) { model.addElement(item); } choiceBox2.setModel(model); } Database database = null; CreateChart createChart = null; Query query = null; String[] tweetAnalyse = { "Most popular area", "Least popular area", "Visitors country", "Visitors region" }; String[] animalAnalyse = { "Most popular animals" }; String[] zooAnalyse = { "Activity", "Tweet content", "Followers" }; String[] weatherAnalyse = { "Weather", "Temperature" }; String selection = "Tweets"; /** * 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() { jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); jMenuBar2 = new javax.swing.JMenuBar(); jMenu3 = new javax.swing.JMenu(); jMenu4 = new javax.swing.JMenu(); jMenuBar3 = new javax.swing.JMenuBar(); jMenu5 = new javax.swing.JMenu(); jMenu6 = new javax.swing.JMenu(); jPanel1 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); jButton3 = new javax.swing.JButton(); choiceMenu = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); choiceBox2 = new javax.swing.JComboBox(); chartLabel = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jMenu1.setText("File"); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); jMenu3.setText("File"); jMenuBar2.add(jMenu3); jMenu4.setText("Edit"); jMenuBar2.add(jMenu4); jMenu5.setText("File"); jMenuBar3.add(jMenu5); jMenu6.setText("Edit"); jMenuBar3.add(jMenu6); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "Week", "Month" })); jComboBox1.setSelectedIndex(2); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jButton3.setText("Update"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); choiceMenu.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Tweets", "Zoo", "Animal", "Weather" })); choiceMenu.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { choiceMenuActionPerformed(evt); } }); choiceBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { choiceBox2ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(22, 22, 22) .addComponent(choiceMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(choiceBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3).addGap(33, 33, 33).addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout .createSequentialGroup().addContainerGap().addComponent(chartLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 604, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jLabel1))); jPanel1Layout .setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(chartLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGap(17, 17, 17) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(choiceMenu, 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) .addComponent(choiceBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton3)) .addContainerGap(84, Short.MAX_VALUE))); jButton1.setText("Log out"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1))); }// </editor-fold>//GEN-END:initComponents private void setCheckbox() { switch (choiceMenu.getSelectedIndex()) { case 0: setCheckBox(tweetAnalyse); selection = "Tweets"; break; case 1: setCheckBox(zooAnalyse); selection = "Zoo"; break; case 2: setCheckBox(animalAnalyse); selection = "Animal"; break; case 3: setCheckBox(weatherAnalyse); selection = "Weather"; break; } showChart(); } private void choiceMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_choiceMenuActionPerformed setCheckbox(); }//GEN-LAST:event_choiceMenuActionPerformed private void showChart() { switch (selection) { case "Tweets": switch (choiceBox2.getSelectedIndex()) { case 0: //pie chart createChart = new CreateChart(query.getBestAsset(), "Most popular asset"); break; case 1: //pie chart createChart = new CreateChart(query.getWorstAsset(), "Least popular asset"); break; case 2: //pie chart createChart = new CreateChart(query.getCountry(), "Country"); break; case 3: //pie chart createChart = new CreateChart(query.getRegion(), "Region"); break; } break; case "Animal": switch (choiceBox2.getSelectedIndex()) { case 0: //Best animal //Barchart createChart = new CreateChart(query.getBestAnimal(), "Tweets about animals", "Animal", "Amount"); break; } break; case "Zoo": switch (choiceBox2.getSelectedIndex()) { case 0: //Bar chart createChart = new CreateChart(query.getBlijdorpTweetsAndDate(), "Response", "Date", "Tweets"); break; case 1: //Pie chart createChart = new CreateChart(query.getBlijdorpTweets(), "Tweet content"); break; case 2: //Bar chart createChart = new CreateChart(query.getFollowersBlijdorp(), "Followers", "Date", "Followers"); break; } break; case "Weather": switch (choiceBox2.getSelectedIndex()) { case 0: //Bar chart createChart = new CreateChart(query.getWeatherAndTweets(), "Tweets and Weather", "Tweets", "Amount"); break; case 1: //Bar Chart createChart = new CreateChart(query.getTemperatureAndTweets(), "Temperature", "Tweets", "Temperatuur"); break; } break; }//end of (selection) switch } private void choiceBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_choiceBox2ActionPerformed showChart(); }//GEN-LAST:event_choiceBox2ActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed changeDateSeach(); }//GEN-LAST:event_jComboBox1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed showChart(); }//GEN-LAST:event_jButton3ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed database.closeDatabase(); System.exit(0); }//GEN-LAST:event_jButton1ActionPerformed private void changeDateSeach() { switch (jComboBox1.getSelectedIndex()) { case 0: query.setDay(); break; case 1: query.setWeek(); break; case 2: query.setMonth(); break; } showChart(); } // Variables declaration - do not modify//GEN-BEGIN:variables public static javax.swing.JLabel chartLabel; private javax.swing.JComboBox choiceBox2; private javax.swing.JComboBox choiceMenu; private javax.swing.JButton jButton1; private javax.swing.JButton jButton3; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenu jMenu4; private javax.swing.JMenu jMenu5; private javax.swing.JMenu jMenu6; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuBar jMenuBar2; private javax.swing.JMenuBar jMenuBar3; private javax.swing.JPanel jPanel1; // End of variables declaration//GEN-END:variables }