Last active
August 29, 2015 13:56
-
-
Save jsuwo/9129747 to your computer and use it in GitHub Desktop.
Starter code for Lab 8.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.uwo.csd.cs2212.USERNAME; | |
| import javax.swing.SwingUtilities; | |
| public class App { | |
| public static void main(String args[]) { | |
| SwingUtilities.invokeLater(new Runnable() { | |
| @Override | |
| public void run() { | |
| MainWindow window = new MainWindow(); | |
| window.setVisible(true); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.uwo.csd.cs2212.USERNAME; | |
| import java.awt.Color; | |
| import java.awt.Component; | |
| import javax.swing.JTable; | |
| import javax.swing.table.DefaultTableCellRenderer; | |
| import java.text.Format; | |
| import java.text.NumberFormat; | |
| import javax.swing.SwingConstants; | |
| public class CurrencyCellRenderer extends DefaultTableCellRenderer { | |
| private final Format formatter; | |
| public CurrencyCellRenderer() { | |
| this.formatter = NumberFormat.getCurrencyInstance(); | |
| // Set horizontal alignment here | |
| } | |
| @Override | |
| public Component getTableCellRendererComponent(JTable table, Object value, | |
| boolean isSelected, boolean hasFocus, int row, int column) { | |
| // Cast the value to a double here. | |
| // If the value is > 0, set the foreground color of the cell to red; | |
| // otherwise, set it to black. | |
| // Format the value with a currency symbol | |
| String formattedValue = formatter.format(amount); | |
| // Set the value here | |
| return this; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.uwo.csd.cs2212.USERNAME; | |
| public class Customer { | |
| private String firstName; | |
| private String lastName; | |
| private String email; | |
| private double balanceOwing; | |
| public Customer(String firstName, String lastName, String email, double balanceOwing) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.email = email; | |
| this.balanceOwing = balanceOwing; | |
| } | |
| public String getFirstName() { | |
| return firstName; | |
| } | |
| public void setFirstName(String firstName) { | |
| this.firstName = firstName; | |
| } | |
| public String getLastName() { | |
| return lastName; | |
| } | |
| public void setLastName(String lastName) { | |
| this.lastName = lastName; | |
| } | |
| public String getEmail() { | |
| return email; | |
| } | |
| public void setEmail(String email) { | |
| this.email = email; | |
| } | |
| public double getBalanceOwing() { | |
| return balanceOwing; | |
| } | |
| public void setBalanceOwing(double balanceOwing) { | |
| this.balanceOwing = balanceOwing; | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append(firstName) | |
| .append(" ") | |
| .append(lastName) | |
| .append(" (") | |
| .append(email) | |
| .append("): ") | |
| .append("$") | |
| .append(balanceOwing); | |
| return sb.toString(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.uwo.csd.cs2212.USERNAME; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import javax.swing.table.AbstractTableModel; | |
| public class CustomerTableModel extends AbstractTableModel { | |
| private final static int COLUMN_COUNT = 4; | |
| private final static int IDX_FIRST_NAME = 0; | |
| private final static int IDX_LAST_NAME = 1; | |
| private final static int IDX_EMAIL = 2; | |
| private final static int IDX_BALANCE = 3; | |
| private final List<Customer> customers; | |
| public CustomerTableModel() { | |
| customers = new ArrayList<>(); | |
| } | |
| public List<Customer> getCustomers() { | |
| return customers; | |
| } | |
| @Override | |
| public int getRowCount() { | |
| return customers.size(); | |
| } | |
| @Override | |
| public int getColumnCount() { | |
| return COLUMN_COUNT; | |
| } | |
| @Override | |
| public Class<?> getColumnClass(int columnIndex) { | |
| return (columnIndex == IDX_BALANCE ? Double.class : String.class); | |
| } | |
| @Override | |
| public String getColumnName(int columnIndex) { | |
| switch (columnIndex) { | |
| case IDX_FIRST_NAME: | |
| return "First Name"; | |
| case IDX_LAST_NAME: | |
| return "Last Name"; | |
| case IDX_EMAIL: | |
| return "Email"; | |
| case IDX_BALANCE: | |
| return "Balance Owing"; | |
| default: | |
| return null; | |
| } | |
| } | |
| @Override | |
| public Object getValueAt(int rowIndex, int columnIndex) { | |
| if ((rowIndex < 0) || (rowIndex >= customers.size())) | |
| return null; | |
| // add implementation here | |
| } | |
| @Override | |
| public void setValueAt(Object aValue, int rowIndex, int columnIndex) { | |
| if ((rowIndex < 0) || (rowIndex >= customers.size()) || columnIndex != IDX_BALANCE) | |
| return; | |
| // add implementation here | |
| } | |
| @Override | |
| public boolean isCellEditable(int rowIndex, int columnIndex) { | |
| // implement me | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.uwo.csd.cs2212.USERNAME; | |
| import java.awt.*; | |
| import java.awt.event.*; | |
| import javax.swing.*; | |
| import javax.swing.event.*; | |
| import javax.swing.table.*; | |
| public class MainWindow extends JFrame { | |
| private JButton btnGetSelection; | |
| private JTable tblCustomers; | |
| private JTextArea txtOutput; | |
| public MainWindow() { | |
| initComponents(); | |
| } | |
| private void initComponents() { | |
| JPanel pnlOutput = new JPanel(); | |
| JScrollPane scrOutput = new JScrollPane(); | |
| JPanel pnlTable = new JPanel(); | |
| JScrollPane scrTable = new JScrollPane(); | |
| JToolBar toolBar = new JToolBar(); | |
| btnGetSelection = new JButton(); | |
| tblCustomers = new JTable(); | |
| txtOutput = new JTextArea(); | |
| setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
| pnlOutput.setBorder(BorderFactory.createTitledBorder("Output")); | |
| pnlOutput.setLayout(new BorderLayout()); | |
| txtOutput.setColumns(20); | |
| txtOutput.setRows(5); | |
| scrOutput.setViewportView(txtOutput); | |
| pnlOutput.add(scrOutput, BorderLayout.CENTER); | |
| getContentPane().add(pnlOutput, BorderLayout.SOUTH); | |
| pnlOutput.getAccessibleContext().setAccessibleDescription(""); | |
| pnlTable.setBorder(BorderFactory.createTitledBorder("Customer Data")); | |
| pnlTable.setLayout(new BorderLayout()); | |
| scrTable.setViewportView(tblCustomers); | |
| pnlTable.add(scrTable, BorderLayout.CENTER); | |
| getContentPane().add(pnlTable, BorderLayout.CENTER); | |
| toolBar.setRollover(true); | |
| btnGetSelection.setText("Get Selection"); | |
| btnGetSelection.setFocusable(false); | |
| btnGetSelection.setHorizontalTextPosition(SwingConstants.CENTER); | |
| btnGetSelection.setVerticalTextPosition(SwingConstants.BOTTOM); | |
| toolBar.add(btnGetSelection); | |
| getContentPane().add(toolBar, BorderLayout.NORTH); | |
| pack(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment