import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; /** * Project #8.6 * CS 2334, Section 001 * October 12, 2003. *

* This program displays two lists of states and allows the user * to move states from one list to another. *

* The following ASCII diagram shows the layout of the components and * container structures. *

*
 *    /-------------------------JFrame--------------------------\
 *    |/-------------------JPanel from JFrame------------------\|
 *    ||                        JLabel                         ||
 *    ||/---------------Upper JPanel (with Border)------------\||
 *    |||/----Left JScrollPane----\ /----Right JScrollPane---\|||
 *    ||||/-------JList----------\| |/-------JList----------\||||
 *    |||||                      || ||                      |||||
 *    |||||                      || ||                      |||||
 *    |||||                      || ||                      |||||
 *    |||||                      || ||                      |||||
 *    ||||\----------------------/| |\----------------------/||||
 *    |||\------------------------/ \------------------------/|||
 *    ||\-----------------------------------------------------/||
 *    ||/---------------Lower JPanel (no Border)--------------\||
 *    |||        /---JButton---\       /---JButton---\        |||
 *    |||        |     <--     |       |     -->     |        |||
 *    |||        \-------------/       \-------------/        |||
 *    ||\-----------------------------------------------------/||
 *    |\-------------------------------------------------------/|
 *    \---------------------------------------------------------/
 * 
*

* @author unknown * @version 1.0 */ public class DynamicStateLists implements ActionListener { /** The main frame of the GUI for this program. */ private JFrame frame; /** List on the left side of the GUI. */ private JList listLeft; /** Scroll pane that holds the list on the left side of the GUI. */ private JScrollPane scrollPaneLeft; /** List on the right side of the GUI. */ private JList listRight; /** Scroll pane that holds the list on the right side of the GUI. */ private JScrollPane scrollPaneRight; /** This label will inform the user that the list contains states. */ private JLabel label; /** Left button, moves a state from the right list to the left list. */ private JButton leftButton; /** Right button, moves a state from the left list to the right list. */ private JButton rightButton; /** Data model for the left list. */ private StateListModel dataLeft; /** Data model for the right list. */ private StateListModel dataRight; /** JPanel that holds the two lists of states. */ private JPanel listPanel; /** JPanel that holds the buttons. */ private JPanel buttonPanel; /** * This constructor creates the view model (GUI) for the program * and registers the event listeners used by the program. *

* Algorithm:
* Set the data model. * Build the tree from the bottom up:
* 1. Build the components that will be used.
* 2. Build the containers to place the components into.
* 4. Register action listeners for the buttons.
* 5. Pack the container and set it to be visible.
*

* @param contents The list of data to display * in a JList. * @param theLabel Some text to display as a header * for the JList. */ public DynamicStateLists(StateListModel left, StateListModel right, String theLabel) { // Set the data model being used. dataLeft = left; dataRight = right; // Build the tree from the bottom up: // 1. Build the components that will be used. // Build the remove buttons. JButton leftButton = new JButton("<--"); leftButton.setActionCommand("left"); JButton rightButton = new JButton("-->"); rightButton.setActionCommand("right"); // Create the JLists, passing the model as the argument. listLeft = new JList(dataLeft.getStatesVector()); listRight = new JList(dataRight.getStatesVector()); // Make the lists scroll. scrollPaneLeft = new JScrollPane(listLeft); scrollPaneRight = new JScrollPane(listRight); // Create the label. label = new JLabel(theLabel); // 2. Build the containers to place the components into. // Create a new JPanel to hold the lists of states. listPanel = new JPanel(new GridLayout(1, 2)); listPanel.add(scrollPaneLeft); listPanel.add(scrollPaneRight); // Set up a titled border on the list panel. TitledBorder title; title = BorderFactory.createTitledBorder("Borders are fun"); listPanel.setBorder(title); // Create a lower JPanel to hold the buttons. buttonPanel = new JPanel(); buttonPanel.add(leftButton); buttonPanel.add(rightButton); // Create the main frame of the program. frame = new JFrame("Some States"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 3. Add the components to the container. frame.getContentPane().add(label, BorderLayout.NORTH); frame.getContentPane().add(listPanel, BorderLayout.CENTER); frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH); // 4. Register action listeners for the buttons. leftButton.addActionListener(this); rightButton.addActionListener(this); // 5. Pack the container and set it to be visible. frame.pack(); frame.setVisible(true); } /** * This method handles an ActionEvent event. It performs an action * that is determined by which button was pressed. *

* @param e This is the ActionEvent created when the * user clicks on one of the buttons * in the GUI. */ public void actionPerformed(ActionEvent e) { // Print out the action to the console. System.out.println(e.getActionCommand()); /* If the user clicked on the left (<---) button * then move the selected item from the right list to * the left list. */ if(e.getActionCommand().equals("left")) { State temp = dataRight.getState(listRight.getSelectedIndex()); if (temp != null) // If the user didn't select an item, do nothing. { dataRight.removeState(listRight.getSelectedIndex()); dataLeft.addState(temp); } // Make sure we update the JLists to reflect the changes. updateLists(); } /* Else, if the user clicked on the right (--->) button * then move the selected item from the left list to * the right list. */ else if (e.getActionCommand().equals("right")) { State temp = dataLeft.getState(listLeft.getSelectedIndex()); if (temp != null) // If the user didn't select an item, do nothing. { dataLeft.removeState(listLeft.getSelectedIndex()); dataRight.addState(temp); } // Make sure we update the JLists to reflect the changes. updateLists(); } } /** * This method refreshees the JLists in the GUI with the current data * in the lists that are part of the data model. *

* Call this method whenever the data in the lists changes. */ private void updateLists() { listLeft.setListData(dataLeft.getStatesVector()); listRight.setListData(dataRight.getStatesVector()); } /** * This method will create the data model for this program and create * an instance of DynamicStateLists in order to invoke the GUI for * the program. *

* @param args Contains the command line arguments. */ public static void main(String[] args) { // Build the data model StateListModel stateListModelL = new StateListModel(); StateListModel stateListModelR = new StateListModel(); // Add data to the data model. stateListModelL.addState(new State("Connecticut", 3405565)); stateListModelL.addState(new State("Maine", 1274923)); stateListModelL.addState(new State("New Mexico", 1819046)); stateListModelL.addState(new State("Iowa", 2926324)); stateListModelL.addState(new State("Colorado", 4301261)); stateListModelL.addState(new State("Wyoming", 493782)); stateListModelL.addState(new State("Delaware", 783600)); stateListModelL.addState(new State("Florida", 15982378)); stateListModelL.addState(new State("Hawaii", 1211537)); stateListModelL.addState(new State("Arizona", 5130632)); stateListModelL.addState(new State("Tennessee", 5689283)); stateListModelL.addState(new State("Texas", 20851820)); stateListModelL.addState(new State("Kansas", 2688418)); stateListModelL.addState(new State("Georgia", 8186453)); stateListModelL.addState(new State("Arkansas", 2673400)); stateListModelL.addState(new State("Nevada", 1998257)); stateListModelL.addState(new State("District of Columbia", 572059)); stateListModelL.addState(new State("Alabama", 4447100)); stateListModelL.addState(new State("Kentucky", 4041769)); stateListModelL.addState(new State("New Hampshire", 1235786)); stateListModelL.addState(new State("South Carolina", 4012012)); stateListModelL.addState(new State("Washington", 5894121)); stateListModelL.addState(new State("New York", 18976457)); stateListModelL.addState(new State("Ohio", 11353140)); stateListModelL.addState(new State("Missouri", 5595211)); stateListModelL.addState(new State("Vermont", 608827)); stateListModelL.addState(new State("Nebraska", 1711263)); stateListModelL.addState(new State("South Dakota", 754844)); stateListModelL.addState(new State("Virginia", 7078515)); stateListModelL.addState(new State("Indiana", 6080485)); stateListModelL.addState(new State("Illinois", 12419293)); stateListModelL.addState(new State("Maryland", 5296486)); stateListModelL.addState(new State("Louisiana", 4468976)); stateListModelL.addState(new State("Alaska", 626932)); stateListModelL.addState(new State("California", 33871648)); stateListModelL.addState(new State("Massachusetts", 6349097)); stateListModelL.addState(new State("New Jersey", 8414350)); stateListModelL.addState(new State("Mississippi", 2844658)); stateListModelL.addState(new State("Idaho", 1293953)); stateListModelL.addState(new State("Michigan", 9938444)); stateListModelL.addState(new State("Pennsylvania", 12281054)); stateListModelL.addState(new State("North Dakota", 642200)); stateListModelL.addState(new State("Minnesota", 4919479)); stateListModelL.addState(new State("Montana", 902195)); stateListModelL.addState(new State("Rhode Island", 1048319)); stateListModelL.addState(new State("Oklahoma", 3450654)); stateListModelL.addState(new State("North Carolina", 8049313)); stateListModelL.addState(new State("West Virginia", 1808344)); stateListModelL.addState(new State("Utah", 2233169)); stateListModelL.addState(new State("Wisconsin", 5363675)); stateListModelL.addState(new State("Oregon", 3421399)); // Invoke the GUI. DynamicStateLists component = new DynamicStateLists(stateListModelL, stateListModelR, "States"); } }