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 com; import java.awt.Color; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.SwingUtilities; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; 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.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author dell */ public class babynamesUI extends javax.swing.JFrame { splash mframe; int top; public babynamesUI() { setVisible(false); mframe = new splash(this); //uncomment on release mframe.doSplashStuff(); initComponents(); this.setLocationRelativeTo(null); } protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) { //System.exit(0); //remove on release int exit = JOptionPane.showConfirmDialog(this, "Are you sure?", "Confirm Exit?", JOptionPane.YES_NO_OPTION); if (exit == JOptionPane.YES_OPTION) { System.exit(0); } } else { super.processWindowEvent(e); } } /** * 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() { buttonGroup1 = new javax.swing.ButtonGroup(); jTextField1 = new javax.swing.JTextField(); buttonGroup2 = new javax.swing.ButtonGroup(); topn = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); year = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); male = new javax.swing.JRadioButton(); female = new javax.swing.JRadioButton(); both = new javax.swing.JRadioButton(); jButton1 = new javax.swing.JButton(); topnum = new javax.swing.JTextField(); nametrend = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); nameentered = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); yearentered = new javax.swing.JTextField(); jButton2 = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); male2 = new javax.swing.JRadioButton(); female2 = new javax.swing.JRadioButton(); jTextField1.setText("jTextField1"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); topn.setBorder(javax.swing.BorderFactory.createTitledBorder("Popular Name")); jLabel2.setText("Number :"); jLabel3.setText("Year :"); year.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { yearActionPerformed(evt); } }); jLabel4.setText("Sex :"); buttonGroup1.add(male); male.setText("Male"); buttonGroup1.add(female); female.setText("Female"); buttonGroup1.add(both); both.setText("Both"); both.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bothActionPerformed(evt); } }); jButton1.setText("OK"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout topnLayout = new javax.swing.GroupLayout(topn); topn.setLayout(topnLayout); topnLayout.setHorizontalGroup(topnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, topnLayout.createSequentialGroup() .addContainerGap() .addGroup(topnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(topnLayout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(80, 80, 80)) .addGroup(topnLayout.createSequentialGroup().addComponent(jLabel4).addGap(20, 20, 20))) .addGroup(topnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(topnLayout.createSequentialGroup().addComponent(male).addGap(18, 18, 18) .addComponent(female)) .addGroup(topnLayout.createSequentialGroup().addGap(9, 9, 9).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18) .addGroup(topnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(both).addComponent(topnum, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(117, 117, 117)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, topnLayout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE).addComponent(jButton1).addGap(113, 113, 113))); topnLayout.setVerticalGroup(topnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(topnLayout.createSequentialGroup().addGap(18, 18, 18).addGroup(topnLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2) .addComponent(jLabel3) .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(topnum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(topnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(male).addComponent(female).addComponent(both)) .addGap(7, 7, 7).addComponent(jButton1).addContainerGap(44, Short.MAX_VALUE))); jLabel2.getAccessibleContext().setAccessibleDescription(""); nametrend.setBorder(javax.swing.BorderFactory.createTitledBorder("Name Trend")); jLabel1.setText("Name : "); nameentered.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nameenteredActionPerformed(evt); } }); jLabel5.setText("Year :"); jButton2.setText("OK"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel6.setText("Sex :"); buttonGroup2.add(male2); male2.setText("Male"); male2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { male2ActionPerformed(evt); } }); buttonGroup2.add(female2); female2.setText("Female"); female2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { female2ActionPerformed(evt); } }); javax.swing.GroupLayout nametrendLayout = new javax.swing.GroupLayout(nametrend); nametrend.setLayout(nametrendLayout); nametrendLayout.setHorizontalGroup(nametrendLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, nametrendLayout.createSequentialGroup() .addContainerGap() .addGroup(nametrendLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(nametrendLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jButton2).addGap(108, 108, 108)) .addGroup(nametrendLayout.createSequentialGroup().addGroup(nametrendLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addGroup(nametrendLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(nametrendLayout.createSequentialGroup().addGap(64, 64, 64) .addComponent(male2).addGap(51, 51, 51) .addComponent(female2).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(nametrendLayout.createSequentialGroup().addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(nameentered, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5).addGap(95, 95, 95))) .addComponent(yearentered, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(108, 108, 108))))); nametrendLayout.setVerticalGroup(nametrendLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(nametrendLayout.createSequentialGroup().addGap(22, 22, 22).addGroup(nametrendLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(nameentered, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(yearentered, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)).addGap(43, 43, 43) .addGroup(nametrendLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(male2).addComponent(female2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton2) .addContainerGap(32, Short.MAX_VALUE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(topn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(nametrend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap(14, Short.MAX_VALUE) .addComponent(topn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(11, 11, 11).addComponent(nametrend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); topn.getAccessibleContext().setAccessibleName("popularity"); nametrend.getAccessibleContext().setAccessibleName("populatitygraph"); pack(); }// </editor-fold>//GEN-END:initComponents private void yearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yearActionPerformed // TODO add your handling code here: }//GEN-LAST:event_yearActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String year1 = year.getText(); String top = topnum.getText(); String sex; if (male.isSelected()) sex = "male"; else if (female.isSelected()) sex = "female"; else sex = "both"; try { int y = Integer.parseInt(year1); int t = Integer.parseInt(top); if (!(y >= 1944 && y <= 2013)) { JOptionPane.showMessageDialog(null, "please enter year between 1994 and 2013"); } else if (t > 100) { JOptionPane.showMessageDialog(null, "please enter number less than 100"); } else { ArrayList<String> name = new ArrayList<String>(); ArrayList<String> value = new ArrayList<String>(); if (sex.equals("male") || sex.equals("female")) { String filename = sex + "_cy" + year1 + "_top.csv"; InputStream is = babynamesUI.class.getResourceAsStream("/resourse/" + filename); BufferedReader br = null; String line = ""; String cvsSplitBy = ","; br = new BufferedReader(new InputStreamReader(is)); line = br.readLine(); int i = 0; while ((line = br.readLine()) != null && i <= t) { // use comma as separator String[] names = line.split(cvsSplitBy); name.add(names[0]); value.add(names[1]); i++; } } else { String filename1 = "male_cy" + year1 + "_top.csv"; String filename2 = "female_cy" + year1 + "_top.csv"; InputStream is1 = babynamesUI.class.getResourceAsStream("/resourse/" + filename1); InputStream is2 = babynamesUI.class.getResourceAsStream("/resourse/" + filename2); BufferedReader br1 = null; BufferedReader br2 = null; String line1 = ""; String line2 = ""; String cvsSplitBy = ","; br1 = new BufferedReader(new InputStreamReader(is1)); br2 = new BufferedReader(new InputStreamReader(is2)); int i = 0, a, b; line1 = br1.readLine(); line1 = br1.readLine(); line2 = br2.readLine(); line2 = br2.readLine(); while (i <= t) { // use comma as separator if (i > 0) { String[] name1 = line1.split(cvsSplitBy); String[] name2 = line2.split(cvsSplitBy); String s1 = name1[1].trim(); String s2 = name2[1].trim(); if (s1.charAt(0) == '"') { a = Integer.parseInt(s1.substring(1, s1.length() - 1)); } else { a = Integer.parseInt(s1); } if (s2.charAt(0) == '"') { b = Integer.parseInt(s2.substring(1, s2.length() - 1)); } else { b = Integer.parseInt(s2); } if (a > b) { name.add(name1[0]); value.add(name1[1]); line1 = br1.readLine(); } else { name.add(name2[0]); value.add(name2[1]); line2 = br2.readLine(); } } i++; } } namelist list = new namelist(this, name, value); list.setVisible(true); setVisible(false); dispose(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "invalid entry"); e.printStackTrace(); } }//GEN-LAST:event_jButton1ActionPerformed private void bothActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bothActionPerformed // TODO add your handling code here: }//GEN-LAST:event_bothActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String name = nameentered.getText(); String y = yearentered.getText(); String sex = null; if (male2.isSelected()) sex = "male"; else if (female2.isSelected()) sex = "female"; int[] Lvalue = new int[100]; int[] Lyear = new int[100]; int j = 0; try { int year = Integer.parseInt(y); int i = year; while (i <= 2013) { String filename = sex + "_cy" + i + "_top.csv"; InputStream is = babynamesUI.class.getResourceAsStream("/resourse/" + filename); BufferedReader br = null; String line = ""; String cvsSplitBy = ","; br = new BufferedReader(new InputStreamReader(is)); line = br.readLine(); while ((line = br.readLine()) != null) { int val; // use comma as separator String[] names = line.split(cvsSplitBy); String s1 = names[1].trim(); String s2 = names[0].trim(); //System.out.println("name:-"+s1); if (s1.charAt(0) == '"') { val = Integer.parseInt(s1.substring(1, s1.length() - 1)); } else { val = Integer.parseInt(s1); } if (s2.charAt(0) == '"') { s2 = s2.substring(1, s2.length() - 1); } //System.out.println("name:-"+s2); if (s2.equalsIgnoreCase(name)) { Lvalue[j] = val; Lyear[j] = i; j++; } } i++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "invalid entry"); e.printStackTrace(); } //graph grp =new graph(this,Lvalue,Lyear,j); //grp.setVisible(true); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); String val = ""; //textArea1.setText("Year | Number of Birth"); for (int p = 0; p < j; p++) { val = Integer.toString(Lyear[p]); dataset.addValue(Lvalue[p], val, val); // textArea1.setText( textArea1.getText()+" \n"+year[i] +" | "+ value[i]); } JFreeChart chart = ChartFactory.createAreaChart("Name Trend", "year", "Number of Birth", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot p = chart.getCategoryPlot(); p.setRangeGridlinePaint(Color.BLACK); ChartFrame frame = new ChartFrame("Chart for Name Trend", chart); frame.setVisible(true); frame.setSize(1200, 700); frame.setLocationRelativeTo(null); setVisible(false); dispose(); }//GEN-LAST:event_jButton2ActionPerformed private void nameenteredActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameenteredActionPerformed // TODO add your handling code here: }//GEN-LAST:event_nameenteredActionPerformed private void male2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_male2ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_male2ActionPerformed private void female2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_female2ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_female2ActionPerformed /** * @param args the command line arguments */ /* public static void main(String args[]) { 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(babynamesUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(babynamesUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(babynamesUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(babynamesUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new babynamesUI().setVisible(true); } }); }*/ // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JRadioButton both; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JRadioButton female; private javax.swing.JRadioButton female2; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; 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.JLabel jLabel6; private javax.swing.JTextField jTextField1; private javax.swing.JRadioButton male; private javax.swing.JRadioButton male2; private javax.swing.JTextField nameentered; private javax.swing.JPanel nametrend; private javax.swing.JPanel topn; private javax.swing.JTextField topnum; private javax.swing.JTextField year; private javax.swing.JTextField yearentered; // End of variables declaration//GEN-END:variables }