Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gui;
- import java.util.ArrayList;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import db.DBDataProvider;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.util.Collections;
- import java.util.Vector;
- import javax.swing.JButton;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.table.DefaultTableModel;
- import model.City;
- import model.Country;
- 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.data.category.DefaultCategoryDataset;
- import org.jfree.data.general.DefaultPieDataset;
- public class CountryViewer implements ItemListener{
- public JFrame frame;
- JComboBox<Country> countryCombo;
- JTable table;
- JButton barchart;
- JButton pichart;
- public CountryViewer(){
- this.frame = new JFrame("DB Data Viewer");
- this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.frame.setBounds(400, 200, 500, 400);
- this.frame.getContentPane().setBackground(Color.cyan);
- this.frame.setVisible(true);
- this.frame.setLayout(null);
- //Add the Country Combobox
- countryCombo = new JComboBox<Country>();
- countryCombo.setEditable(true);
- this.frame.add(countryCombo);
- this.countryCombo.setBounds(150, 15, 200, 20);
- barchart = new JButton();
- this.barchart.setText("Population BarChart");
- this.frame.add(barchart);
- this.barchart.setBounds(60,260, 150, 25);
- pichart = new JButton();
- this.pichart.setText("Population PiChart");
- this.frame.add(pichart);
- this.pichart.setBounds(270,260, 150, 25);
- pichart.addItemListener(this);
- //Add Country information to the combo box by addItem method
- ArrayList<Country> cList = DBDataProvider.getCountryList();
- Collections.sort(cList);
- for(Country c:cList){
- this.countryCombo.addItem(c);
- }
- Vector<Vector<String>> data = new Vector<Vector<String>>();
- Vector<String> rowData = new Vector<String>();
- rowData.add("C1R1");rowData.add("C2R1");rowData.add("C3R1");
- rowData.add("C4R1");rowData.add("C5R1");
- data.add(rowData);
- Vector<String> columnNames = new Vector<String>();
- columnNames.add("ID");columnNames.add("Name");columnNames.add("CountryCode");
- columnNames.add("District");columnNames.add("Population");
- this.table = new JTable(data, columnNames);
- JScrollPane scrolPane = new JScrollPane(table);
- scrolPane.setBounds(40,80,400,150);
- this.frame.add(scrolPane);
- this.countryCombo.addItemListener(this);
- // barchart.addItemListener(this);
- // pichart.addItemListener(this);
- }
- @Override
- public void itemStateChanged(ItemEvent arg0) {
- DefaultTableModel dtm = (DefaultTableModel)this.table.getModel();
- dtm.getDataVector().clear();
- Country c = (Country)this.countryCombo.getSelectedItem();
- ArrayList<City> cityList = DBDataProvider.getCityList(c);
- for(City city :cityList){
- Vector<String> rowData = new Vector<String>();
- rowData.add(city.getId());
- rowData.add(city.getName());
- rowData.add(city.getCountryCode());
- rowData.add(city.getDistrict());
- rowData.add(city.getPopulation());
- dtm.addRow(rowData);
- }
- barchart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- DefaultCategoryDataset barchartData = new DefaultCategoryDataset();
- for(City city :cityList){
- barchartData.setValue(Integer.parseInt(city.getPopulation()), "Population", city.getName());
- }
- JFreeChart chart = ChartFactory.createBarChart("City Population Chart", "City Name", "Population", barchartData);
- //JFreeChart chart = ChartFactory.createBarChart("City Population Chart", "City Name", "Population", dfd, PlotOrientation.VERTICAL, false, true, false);
- CategoryPlot cp = chart.getCategoryPlot();
- cp.setRangeGridlinePaint(Color.RED);
- ChartFrame barChartFrame = new ChartFrame("Barchart for Population",chart);
- barChartFrame.setVisible(true);
- barChartFrame.setBounds(50, 50, 800, 500);
- }
- });
- pichart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- DefaultPieDataset pichartData = new DefaultPieDataset();
- for(City city :cityList){
- pichartData.setValue(city.getName(),new Integer(Integer.parseInt(city.getPopulation())));
- }
- JFreeChart chart = ChartFactory.createPieChart("City Population Piechart", pichartData);
- PiePlot p = (PiePlot)chart.getPlot();
- ChartFrame pieChartFrame = new ChartFrame("Piechart for Population",chart);
- pieChartFrame.setVisible(true);
- pieChartFrame.setBounds(50, 50, 600, 600);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment