import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.beans.*; /** * DoubleListMain puts together an application that records numbers * into a list and reports their average and max. * * @author Stuart Hansen * @version September 2008 */ public class DoubleListMain extends JFrame { private JScrollPane pane; private JList list; // the list's display private DoubleListModel model; // the model holding the list private JLabel add; // the label for the add field private JTextField inputField; // where the numbers are entered private JLabel errorLabel; // a label to display error messages private JLabel avgLabel; // where the average is displayed private Average avg; // calculates the average private JLabel maxLabel; // where the max is displayed private Max maximum; // finds the maximum private JButton del; // deletes the selected element private JButton clear; // clears the entire list // The constructor "wires" together the application public DoubleListMain () { Container cPane = getContentPane(); cPane.setLayout(null); // Set up the list and its model model = new DoubleListModel(); list = new JList(); list.setModel(model); pane = new JScrollPane(list); pane.setSize(100, 150); pane.setLocation (100, 150); cPane.add(pane); // Set up the label for the add field add = new JLabel("Add"); add.setLocation(50, 50); add.setSize(40,30); cPane.add(add); // Set up the input textField inputField = new JTextField(); inputField.setLocation(100, 50); inputField.setSize(100, 30); inputField.addActionListener(new AddHandler()); cPane.add(inputField); // Set up the label for error messages errorLabel = new JLabel(""); errorLabel.setForeground(Color.red); errorLabel.setSize(400, 30); errorLabel.setLocation(50, 100); cPane.add(errorLabel); // Set up the delete button del = new JButton("Delete"); del.setSize (100, 30); del.setLocation(210, 50); del.addActionListener(new DeleteHandler()); cPane.add(del); // Set up the clear button clear = new JButton("Clear All"); clear.setSize (100, 30); clear.setLocation(320, 50); clear.addActionListener(new ClearHandler()); cPane.add(clear); // Set up the average value and label avg = new Average(); avgLabel = new JLabel("Average = NaN"); avgLabel.setSize(200, 30); avgLabel.setLocation(260, 150); list.getModel().addListDataListener(new AverageAdapter()); avg.addPropertyChangeListener(new AvgPropHandler()); cPane.add(avgLabel); // Set up the maximum value and label maximum = new Max(); maxLabel = new JLabel("Maximum = NaN"); maxLabel.setSize(200, 30); maxLabel.setLocation(260, 200); list.getModel().addListDataListener(new MaximumAdapter()); maximum.addPropertyChangeListener(new MaxPropHandler()); cPane.add(maxLabel); // Set a few windowing parameters and show the frame. setSize(500, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } // This class is the handler for adding numbers private class AddHandler implements ActionListener { public void actionPerformed (ActionEvent e) { try { String str = inputField.getText(); model.addElement(str); inputField.setText(""); errorLabel.setText(""); } catch (Exception ex) { errorLabel.setText("Must only add numbers to the list."); } } } // This class is the handler for removing numbers private class DeleteHandler implements ActionListener { public void actionPerformed (ActionEvent e) { try { int index = list.getSelectedIndex(); model.remove(index); errorLabel.setText(""); } catch (Exception ex) { errorLabel.setText("Error! Use mouse to select element to remove"); } } } // This class is the handler for clearing the list private class ClearHandler implements ActionListener { public void actionPerformed (ActionEvent e) { model.clear(); } } // This class updates the average private class AverageAdapter implements ListDataListener { public void contentsChanged(ListDataEvent e) {} public void intervalAdded(ListDataEvent e) { Double[] temp = model.toArray(); avg.findAverage(temp); } public void intervalRemoved(ListDataEvent e) { Double[] temp = model.toArray(); avg.findAverage(temp); } } // This class updates the maximum private class MaximumAdapter implements ListDataListener { public void contentsChanged(ListDataEvent e) {} public void intervalAdded(ListDataEvent e) { Double [] temp = model.toArray(); maximum.findMax(temp); } public void intervalRemoved(ListDataEvent e) { Double [] temp = model.toArray(); maximum.findMax(temp); } } // This handler updates the JLabel when the average changes private class AvgPropHandler implements PropertyChangeListener { public void propertyChange (PropertyChangeEvent e) { avgLabel.setText("Average = " + avg.getAverage()); } } // This handler updates the JLabel when the maximum changes private class MaxPropHandler implements PropertyChangeListener { public void propertyChange (PropertyChangeEvent e) { maxLabel.setText("Maximum = " + maximum.getMaximum()); } } // A one line main program public static void main (String [] args) { new DoubleListMain(); } }