Develop a user interface for displaying the medals table as part of our Winter Olympics
Application. It should be a JFrame and should have the following
Download full brief here
Download zip of all source code for 'Olympic Medals' here
MainApplication is the main class:
Download source code for 'MainApplication' here
// Start of code--------------------- import java.util.ArrayList; import Model.Country; /* Usage notes: * Redistribution, alteration and use of this code with or without * modification, are permitted provided that the following conditions are met: * In no event shall the copyright owner be liable for any direct, indirect, * incidental, special, exemplary, or consequential damages arising in any * way out of the use of this software, even if advised of the possibility * of such damage. */ public class MainApplication { public static void main(String[] args) { //Initialisation of arraylist of countries ArrayList //Create instances of the country class Country russia = new Country("Russia", 13, 11, 9); countries.add(russia); Country norway = new Country("Norway", 11, 5, 10); countries.add(norway); Country canada = new Country("Canada", 10, 10, 5); countries.add(canada); // Setting up the MedalsTableFrame MedalsTableFrame mainFrame = new MedalsTableFrame("Medals Table", countries); mainFrame.setSize(800, 600); mainFrame.setVisible(true); } }//End of code--------------------- |
MedalsTableFrame:
Download source code for 'MedalsTableFrame' here
// Start of code--------------------- import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import Model.Country; /* Usage notes: * Redistribution, alteration and use of this code with or without * modification, are permitted provided that the following conditions are met: * In no event shall the copyright owner be liable for any direct, indirect, * incidental, special, exemplary, or consequential damages arising in any * way out of the use of this software, even if advised of the possibility * of such damage. */ public class MedalsTableFrame extends JFrame { private JPanel mainPanel; private JButton okButton; private JButton cancelButton; private JButton helpButton; private JButton viewScheduleButton; private JButton recordsSetButton; private JLabel sortByLabel; private JComboBox sortByCombo; // Medal table frame public MedalsTableFrame(String title, ArrayList super(title); this.mainPanel = new JPanel(); this.mainPanel.setLayout(new BorderLayout()); getContentPane().add(this.mainPanel); this.mainPanel.add(createBottomButtonPanel(), BorderLayout.SOUTH); this.mainPanel.add(createSideButtonPanel(), BorderLayout.WEST); this.mainPanel.add(createSortByPanel(), BorderLayout.NORTH); this.mainPanel.add(createTable(countries), BorderLayout.CENTER); } // Method to create table to display CountryTableModel private JScrollPane createTable(ArrayList JTable table = new JTable(); CountryTableModel myTableModel = new CountryTableModel(countries); table.setModel(myTableModel); JScrollPane scroller = new JScrollPane(table); return scroller; } // Method to create button panel at bottom of frame private JPanel createBottomButtonPanel() { JPanel bottomPanel = new JPanel(); okButton = new JButton("OK"); cancelButton = new JButton("Cancel"); helpButton = new JButton("Help"); ButtonListener okButtonListener = new ButtonListener(this); okButton.addActionListener(okButtonListener); ButtonListener helpButtonListener = new ButtonListener(this); helpButton.addActionListener(helpButtonListener); bottomPanel.add(okButton); bottomPanel.add(cancelButton); bottomPanel.add(helpButton); return bottomPanel; } // Method to create button panel at side of frame private JPanel createSideButtonPanel() { JPanel sideButtonPanel = new JPanel(); BoxLayout boxLeft = new BoxLayout(sideButtonPanel, BoxLayout.Y_AXIS); sideButtonPanel.setLayout(boxLeft); viewScheduleButton = new JButton("View Schedule"); recordsSetButton = new JButton("Records Set"); sideButtonPanel.add(viewScheduleButton); sideButtonPanel.add(Box.createVerticalStrut(5)); sideButtonPanel.add(recordsSetButton); return sideButtonPanel; } // Method to create combo box for sort by list private JPanel createSortByPanel() { JPanel sortByPanel = new JPanel(); sortByLabel = new JLabel("Sort BY"); String[] sortBy = { "Country Name", "Gold Medals Won", "Silver Medals Won", "Bronze Medals Won", "Total Medals Won" }; sortByCombo = new JComboBox(sortBy); sortByPanel.add(sortByLabel); sortByPanel.add(sortByCombo); return sortByPanel; } // Button / Action listener private class ButtonListener implements ActionListener { private JFrame outerFrame; public ButtonListener(JFrame outerFrame) { this.outerFrame = outerFrame; } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == okButton) { String quitMessage = "Do you want to quit?"; int answer = JOptionPane.showConfirmDialog(outerFrame, quitMessage); if (answer == JOptionPane.YES_OPTION) { System.exit(0); // User clicked YES - Close appliaction } else if (answer == JOptionPane.NO_OPTION) { // User clicked NO or CANCEL - return to application } } else if (source == helpButton) { String helpMessage = "This is the Help Section"; JOptionPane.showMessageDialog(outerFrame, helpMessage); } } } }//End of code--------------------- |
CountryTableModel:
Download source code for 'CountryTableModel' here
// Start of code--------------------- import java.util.ArrayList; import javax.swing.table.DefaultTableModel; import Model.Country; /* Usage notes: * Redistribution, alteration and use of this code with or without * modification, are permitted provided that the following conditions are met: * In no event shall the copyright owner be liable for any direct, indirect, * incidental, special, exemplary, or consequential damages arising in any * way out of the use of this software, even if advised of the possibility * of such damage. */ public class CountryTableModel extends DefaultTableModel { //Private final ints for CountryTableModel private static final int NO_OF_COLS = 5; private static final int COUNTRY_COL = 0; private static final int GOLD_MEDAL_COL = 1; private static final int SILVER_MEDAL_COL = 2; private static final int BRONZE_MEDAL_COL = 3; private static final int TOTAL_MEDALS_COL = 4; private ArrayList // Method used to dedicate title names to columns public CountryTableModel(ArrayList this.countries = countries; } // Return number of columns in table public int getColumnCount() { return NO_OF_COLS; } // Method used to dedicate title names to columns public String getColumnName(int column) { if (column == COUNTRY_COL) { return "Country"; } else if (column == GOLD_MEDAL_COL) { return "Gold"; } else if (column == SILVER_MEDAL_COL) { return "Silver"; } else if (column == BRONZE_MEDAL_COL) { return "Bronze"; } else if (column == TOTAL_MEDALS_COL) { return "Total"; } else { return ""; } } // Method used to get number of table rows from the number of elements in arraylist public int getRowCount() { if (this.countries != null) { return this.countries.size(); } else { return 0; } } // Method used to position int value of medals into dedicated columns public Object getValueAt(int row, int col) { Country countryToGet = this.countries.get(row); if (col == COUNTRY_COL) { return countryToGet.getnameOfCountry(); } else if (col == GOLD_MEDAL_COL) { return countryToGet.getnoOfGoldMedals(); } else if (col == SILVER_MEDAL_COL) { return countryToGet.getnoOfSilverMedals(); } else if (col == BRONZE_MEDAL_COL) { return countryToGet.getnoOfBronzeMedals(); } else if (col == TOTAL_MEDALS_COL) { return countryToGet.getTotalMedals(); } else { return ""; } } } //End of code--------------------- |
Country class (Model Folder):
Download source code for 'Country' here
// Start of code--------------------- package Model; import java.util.ArrayList; /* Usage notes: * Redistribution, alteration and use of this code with or without * modification, are permitted provided that the following conditions are met: * In no event shall the copyright owner be liable for any direct, indirect, * incidental, special, exemplary, or consequential damages arising in any * way out of the use of this software, even if advised of the possibility * of such damage. */ public class Country { // private int for the Country class private int noOfGoldMedals; private int noOfSilverMedals; private int noOfBronzeMedals; private String nameOfCountry; ArrayList // Constructer for the Country class public Country(String nameOfCountry, int noOfGoldMedals, int noOfSilverMedals, int noOfBronzeMedals) { this.nameOfCountry = nameOfCountry; this.noOfGoldMedals = noOfGoldMedals; this.noOfSilverMedals = noOfSilverMedals; this.noOfBronzeMedals = noOfBronzeMedals; this.countries = new ArrayList } // Method used to add country public void addCountry(Country country) { this.countries.add(country); } // Method used to get country name public String getnameOfCountry() { return nameOfCountry; } // Method used to set country name public void setnameOfCountry(String nameOfCountry) { this.nameOfCountry = nameOfCountry; } // Getters and setter of medals public int getnoOfGoldMedals() { return noOfGoldMedals; } public void setnoOfGoldMedals(int noOfGoldMedals) { this.noOfGoldMedals = noOfGoldMedals; } public int getnoOfSilverMedals() { return noOfSilverMedals; } public void setnoOfSilverMedals(int noOfSilverMedals) { this.noOfSilverMedals = noOfSilverMedals; } public int getnoOfBronzeMedals() { return noOfBronzeMedals; } public void setnoOfBronzeMedals(int noOfBronzeMedals) { this.noOfBronzeMedals = noOfBronzeMedals; } //end of getters and setters of medals //Return int value of total country medals by adding all medals for each country public int getTotalMedals() { return noOfGoldMedals + noOfSilverMedals + noOfBronzeMedals; } } //End of code--------------------- |
0 comments:
Post a Comment
Please feel free to leave comments or ask questions related to the tutorials.