SabirSazzad

Country Viewer Class

Apr 13th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1.  
  2. package gui;
  3.  
  4. import java.util.ArrayList;
  5. import javax.swing.JComboBox;
  6. import javax.swing.JFrame;
  7. import db.DBDataProvider;
  8. import java.awt.Color;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.ItemEvent;
  12. import java.awt.event.ItemListener;
  13. import java.util.Collections;
  14. import java.util.Vector;
  15. import javax.swing.JButton;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTable;
  18. import javax.swing.table.DefaultTableModel;
  19. import model.City;
  20. import model.Country;
  21. import org.jfree.chart.ChartFactory;
  22. import org.jfree.chart.ChartFrame;
  23. import org.jfree.chart.JFreeChart;
  24. import org.jfree.chart.plot.CategoryPlot;
  25. import org.jfree.chart.plot.PiePlot;
  26. import org.jfree.data.category.DefaultCategoryDataset;
  27. import org.jfree.data.general.DefaultPieDataset;
  28.  
  29. public class CountryViewer implements ItemListener{
  30.    
  31.     public JFrame frame;
  32.     JComboBox<Country> countryCombo;
  33.     JTable table;
  34.     JButton barchart;
  35.     JButton pichart;
  36.    
  37.     public CountryViewer(){
  38.         this.frame = new JFrame("DB Data Viewer");
  39.         this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40.         this.frame.setBounds(400, 200, 500, 400);
  41.         this.frame.getContentPane().setBackground(Color.cyan);
  42.         this.frame.setVisible(true);
  43.         this.frame.setLayout(null);
  44.        
  45.         //Add the Country Combobox
  46.         countryCombo = new JComboBox<Country>();
  47.         countryCombo.setEditable(true);
  48.         this.frame.add(countryCombo);
  49.         this.countryCombo.setBounds(150, 15, 200, 20);
  50.        
  51.         barchart = new JButton();
  52.         this.barchart.setText("Population BarChart");
  53.         this.frame.add(barchart);
  54.         this.barchart.setBounds(60,260, 150, 25);
  55.        
  56.         pichart = new JButton();
  57.         this.pichart.setText("Population PiChart");
  58.         this.frame.add(pichart);
  59.         this.pichart.setBounds(270,260, 150, 25);
  60.         pichart.addItemListener(this);
  61.        
  62.         //Add Country information to the combo box by addItem method
  63.         ArrayList<Country> cList = DBDataProvider.getCountryList();
  64.         Collections.sort(cList);
  65.         for(Country c:cList){
  66.             this.countryCombo.addItem(c);
  67.         }          
  68.        
  69.         Vector<Vector<String>> data = new Vector<Vector<String>>();
  70.        
  71.         Vector<String> rowData = new Vector<String>();
  72.         rowData.add("C1R1");rowData.add("C2R1");rowData.add("C3R1");
  73.         rowData.add("C4R1");rowData.add("C5R1");
  74.         data.add(rowData);
  75.        
  76.         Vector<String> columnNames = new Vector<String>();
  77.         columnNames.add("ID");columnNames.add("Name");columnNames.add("CountryCode");
  78.         columnNames.add("District");columnNames.add("Population");
  79.        
  80.         this.table = new JTable(data, columnNames);
  81.        
  82.         JScrollPane scrolPane = new JScrollPane(table);
  83.         scrolPane.setBounds(40,80,400,150);
  84.         this.frame.add(scrolPane);
  85.        
  86.         this.countryCombo.addItemListener(this);
  87. //        barchart.addItemListener(this);
  88. //        pichart.addItemListener(this);
  89.  
  90.                
  91.     }
  92.    
  93.     @Override
  94.     public void itemStateChanged(ItemEvent arg0) {
  95.  
  96.         DefaultTableModel dtm = (DefaultTableModel)this.table.getModel();
  97.         dtm.getDataVector().clear();
  98.         Country c = (Country)this.countryCombo.getSelectedItem();
  99.         ArrayList<City> cityList = DBDataProvider.getCityList(c);
  100.         for(City city :cityList){
  101.             Vector<String> rowData = new Vector<String>();
  102.             rowData.add(city.getId());          
  103.             rowData.add(city.getName());
  104.             rowData.add(city.getCountryCode());
  105.             rowData.add(city.getDistrict());
  106.             rowData.add(city.getPopulation());
  107.             dtm.addRow(rowData);
  108.         }
  109.         barchart.addActionListener(new ActionListener() {
  110.         @Override
  111.         public void actionPerformed(ActionEvent e) {
  112.               DefaultCategoryDataset barchartData = new DefaultCategoryDataset();
  113.               for(City city :cityList){
  114.                   barchartData.setValue(Integer.parseInt(city.getPopulation()), "Population", city.getName());
  115.               }
  116.               JFreeChart chart = ChartFactory.createBarChart("City Population Chart", "City Name", "Population", barchartData);
  117.               //JFreeChart chart = ChartFactory.createBarChart("City Population Chart", "City Name", "Population", dfd, PlotOrientation.VERTICAL, false, true, false);
  118.               CategoryPlot cp = chart.getCategoryPlot();
  119.               cp.setRangeGridlinePaint(Color.RED);
  120.               ChartFrame barChartFrame = new ChartFrame("Barchart for Population",chart);
  121.               barChartFrame.setVisible(true);
  122.               barChartFrame.setBounds(50, 50, 800, 500);
  123.              
  124.         }
  125.         });
  126.         pichart.addActionListener(new ActionListener() {
  127.         @Override
  128.         public void actionPerformed(ActionEvent e) {
  129.               DefaultPieDataset pichartData = new DefaultPieDataset();
  130.               for(City city :cityList){
  131.                   pichartData.setValue(city.getName(),new Integer(Integer.parseInt(city.getPopulation())));
  132.               }
  133.               JFreeChart chart = ChartFactory.createPieChart("City Population Piechart", pichartData);
  134.               PiePlot p = (PiePlot)chart.getPlot();
  135.               ChartFrame pieChartFrame = new ChartFrame("Piechart for Population",chart);
  136.               pieChartFrame.setVisible(true);
  137.               pieChartFrame.setBounds(50, 50, 600, 600);
  138.              
  139.         }
  140.         });
  141.     }
  142.    
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment