Source of MultipleSelectionFrame.java


  1: // Fig. 11.25: MultipleSelectionFrame.java
  2: // Copying items from one List to another.
  3: import java.awt.FlowLayout;
  4: import java.awt.event.ActionListener;
  5: import java.awt.event.ActionEvent;
  6: import javax.swing.JFrame;
  7: import javax.swing.JList;
  8: import javax.swing.JButton;
  9: import javax.swing.JScrollPane;
 10: import javax.swing.ListSelectionModel;
 11: 
 12: public class MultipleSelectionFrame extends JFrame 
 13: {
 14:    private JList colorJList; // list to hold color names
 15:    private JList copyJList; // list to copy color names into
 16:    private JButton copyJButton; // button to copy selected names
 17:    private final String colorNames[] = { "Black", "Blue", "Cyan", 
 18:       "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", 
 19:       "Pink", "Red", "White", "Yellow" };
 20: 
 21:    // MultipleSelectionFrame constructor
 22:    public MultipleSelectionFrame()
 23:    {
 24:       super( "Multiple Selection Lists" );
 25:       setLayout( new FlowLayout() ); // set frame layout
 26: 
 27:       colorJList = new JList( colorNames ); // holds names of all colors
 28:       colorJList.setVisibleRowCount( 5 ); // show five rows
 29:       colorJList.setSelectionMode( 
 30:          ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
 31:       add( new JScrollPane( colorJList ) ); // add list with scrollpane
 32: 
 33:       copyJButton = new JButton( "Copy >>>" ); // create copy button
 34:       copyJButton.addActionListener(
 35: 
 36:          new ActionListener() // anonymous inner class 
 37:          {  
 38:             // handle button event
 39:             public void actionPerformed( ActionEvent event )
 40:             {
 41:                // place selected values in copyJList
 42:                copyJList.setListData( colorJList.getSelectedValues() );
 43:             } // end method actionPerformed
 44:          } // end anonymous inner class
 45:       ); // end call to addActionListener
 46: 
 47:       add( copyJButton ); // add copy button to JFrame
 48: 
 49:       copyJList = new JList(); // create list to hold copied color names
 50:       copyJList.setVisibleRowCount( 5 ); // show 5 rows
 51:       copyJList.setFixedCellWidth( 100 ); // set width
 52:       copyJList.setFixedCellHeight( 15 ); // set height
 53:       copyJList.setSelectionMode( 
 54:          ListSelectionModel.SINGLE_INTERVAL_SELECTION );
 55:       add( new JScrollPane( copyJList ) ); // add list with scrollpane
 56:    } // end MultipleSelectionFrame constructor
 57: } // end class MultipleSelectionFrame
 58: 
 59: 
 60: /**************************************************************************
 61:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 62:  * Pearson Education, Inc. All Rights Reserved.                           *
 63:  *                                                                        *
 64:  * DISCLAIMER: The authors and publisher of this book have used their     *
 65:  * best efforts in preparing the book. These efforts include the          *
 66:  * development, research, and testing of the theories and programs        *
 67:  * to determine their effectiveness. The authors and publisher make       *
 68:  * no warranty of any kind, expressed or implied, with regard to these    *
 69:  * programs or to the documentation contained in these books. The authors *
 70:  * and publisher shall not be liable in any event for incidental or       *
 71:  * consequential damages in connection with, or arising out of, the       *
 72:  * furnishing, performance, or use of these programs.                     *
 73:  *************************************************************************/