/* Swing */ import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.JButton; import javax.swing.ImageIcon; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.border.TitledBorder; import javax.swing.BorderFactory; import javax.swing.JTable; import javax.swing.ListSelectionModel; /* AWT */ import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.BorderLayout; import javax.swing.BoxLayout; import java.net.URL; import java.awt.Color; import java.awt.Dimension; import javax.swing.table.DefaultTableModel; /* Beans */ import beans.jw.Customer; /* CustomerList.java ================= This is one of many typical JPanels that make up the GUI. It displays a list of customers in the database. There are 3 main functions: 1) loadGui: Constructs the graphical components and adds them to the display of this panel. 2) populateGui: Populates and changes the value of the graphical components already loaded onto the JPanel. This is required since the gui must represent changes the user makes to the data. For example, if the user deletes a customer, then the GUI must display one less custmer. For this reason we separate the construction of the gui, from the population of values. 3) actionPerfromed: A simple listener for the buttons used on this Panel. We could* contruct the GUI after we have loaded all the database data in, then there we be no need to separate GUI creation from GUI population. However this would not allow updating of GUI data as the user makes 'live' changes. */ class CustomerList extends JPanel implements ActionListener, Settings, Modes { private XInterface front; /* Initial values of Table. data[][] is never actually populated, it's used because when this gui is actually constructed, we don't yet know how many customers there are. */ String[] columnNames = {"Customer name", "Accounts"}; private Object[][] data; private JTable jTable; private JPanel displayPanel, actionPanel, innerActionPanel; private JScrollPane scrollPane; private TitledBorder title; private JButton btnLogout, btnEdit, btnExpunge; private DefaultTableModel model; public CustomerList(XInterface frontEnd) { super(new BorderLayout()); /* Set global reference to front end. */ front = frontEnd; /* Use system look and feel. */ try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); } catch (Exception e) { } /* Create the panel the use will see. */ createGui( ); } private void createGui() { actionPanel = new JPanel( new BorderLayout() ); displayPanel = new JPanel( new FlowLayout( FlowLayout.CENTER, 0, 70) ); innerActionPanel = new JPanel( new FlowLayout() ); model = new DefaultTableModel(data, columnNames); jTable = new JTable(model); scrollPane = new JScrollPane(jTable); scrollPane.setPreferredSize(new Dimension(500, 200)); btnLogout = new JButton("Logout"); btnEdit = new JButton("Edit"); btnExpunge = new JButton("Expunge"); /* Create the border and title for the splash screen. */ title = BorderFactory.createTitledBorder("Customer Listing"); /* Make sure our buttons are the same size using predefined Dimension. */ btnLogout.setPreferredSize(BUTTON_SIZE); btnEdit.setPreferredSize(BUTTON_SIZE); btnExpunge.setPreferredSize(BUTTON_SIZE); btnLogout.addActionListener(this); btnEdit.addActionListener(this); btnExpunge.addActionListener(this); /* Set a size for splash screen using predefined Dimension. */ setPreferredSize(PANEL_SIZE); displayPanel.setBorder(title); displayPanel.add(scrollPane); /* Add our confirm and cancel buttons to the action panel (accurate positioning), and then the outer (for general positioning). */ innerActionPanel.add(btnEdit); innerActionPanel.add(btnExpunge); innerActionPanel.add(btnLogout); actionPanel.add(innerActionPanel, BorderLayout.EAST); /* Add two main panels to splash panel. */ add(displayPanel, BorderLayout.CENTER); add(actionPanel, BorderLayout.PAGE_END); } public void populateGui() { Customer tmpCustomer; /* Remove all rows to stop new ones concatinating onto the old ones. */ model.setNumRows(0); /* Populate the Table with a list of customers. */ for (int i=0;i < CustomerManager.getNumberCustomers() ; i++) { tmpCustomer = CustomerManager.getCustomer(i); model.addRow(createRow(tmpCustomer.getName(), tmpCustomer.getNumberAccounts())); } /* If the table has at leasr one row, then make the first row selected by default. */ if (jTable.getRowCount() >= 1) jTable.addRowSelectionInterval(0, 0); /* Make sure we have a nice looking table. */ jTable.getTableHeader().setReorderingAllowed(false); jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTable.setGridColor(Color.BLACK); jTable.setSelectionBackground(new Color(102,153,102)); jTable.setRowHeight(25); jTable.setShowVerticalLines(false); jTable.addRowSelectionInterval(0, 0); } /* Used to construct rows dynamically for the Table (model). */ private Object[] createRow(String name, int accounts) { Object[] newRow = new Object[2]; int row = jTable.getRowCount() + 1; newRow[0] = " " + name; newRow[1] = " " + Integer.toString( accounts ); return newRow; } /* Listeners ========= These are kept as short as possible (sometimes at the expense of readability) to prevent any locking of the interface. */ public void actionPerformed( ActionEvent e ) { if (e.getSource() == btnEdit) { /* Flag the selected record as opened by the user. */ CustomerManager.use( jTable.getSelectedRow() ); /* Show the next Panel. */ front.show(ACCOUNT_LIST); } else if (e.getSource() == btnLogout) { front.setLoggedIn(false); front.show(SPLASH); } else if (e.getSource() == btnExpunge) { if (jTable.getSelectedRow() >= 0) { /* Remove customer from the CustomerManager vector. */ CustomerManager.expunge( jTable.getSelectedRow() ); /* Update the table to show changes. */ model.removeRow( jTable.getSelectedRow() ); } } } }