Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.39 KB | None | 0 0
  1. // Fig. 12.15: MultipleSelection.java
  2. // Copying items from one List to another.
  3.  
  4. // Java core packages
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. // Java extension packages
  9. import javax.swing.*;
  10.  
  11. public class MultipleSelection extends JFrame {
  12.    private JList colorList, copyList;
  13.    private JButton copyButton;
  14.    
  15.    private String colorNames[] = { "Black", "Blue", "Cyan",
  16.       "Dark Gray", "Gray", "Green", "Light Gray",
  17.       "Magenta", "Orange", "Pink", "Red", "White", "Yellow" };
  18.  
  19.    // set up GUI
  20.    public MultipleSelection()
  21.    {
  22.       super( "Multiple Selection Lists" );
  23.  
  24.       // get content pane and set its layout
  25.       Container container = getContentPane();
  26.       container.setLayout( new FlowLayout() );
  27.  
  28.       // set up JList colorList
  29.       colorList = new JList( colorNames );
  30.       colorList.setVisibleRowCount( 8 );
  31.       colorList.setFixedCellHeight( 15 );
  32.       colorList.setSelectionMode(
  33.          ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
  34.       container.add( new JScrollPane( colorList ) );
  35.  
  36.       // create copy button and register its listener
  37.       copyButton = new JButton( "Copy >>>" );
  38.  
  39.       copyButton.addActionListener(
  40.  
  41.          // anonymous inner class for button event
  42.          new ActionListener() {
  43.  
  44.             // handle button event
  45.             public void actionPerformed( ActionEvent event )
  46.             {  
  47.                // place selected values in copyList
  48.                copyList.setListData(
  49.                        colorList.getSelectedValues()
  50.                );
  51.             }
  52.  
  53.          }  // end anonymous inner class
  54.  
  55.       ); // end call to addActionListener
  56.  
  57.       container.add( copyButton );
  58.  
  59.       // set up JList copyList
  60.       copyList = new JList( );
  61.       //copyList.setVisibleRowCount( 7 );
  62.       copyList.setFixedCellWidth( 120 );
  63.       copyList.setFixedCellHeight( 15 );
  64.       copyList.setSelectionMode(
  65.          ListSelectionModel.SINGLE_INTERVAL_SELECTION );
  66.       container.add( new JScrollPane( copyList ) );
  67.  
  68.       setSize( 370, 170 );
  69.       setVisible( true );
  70.    }
  71.  
  72.    // execute application
  73.    public static void main( String args[] )
  74.    {
  75.       MultipleSelection application = new MultipleSelection();
  76.  
  77.       application.setDefaultCloseOperation(
  78.          JFrame.EXIT_ON_CLOSE );
  79.    }
  80.  
  81. }  // end class MultipleSelection
  82.  
  83.  
  84. /**************************************************************************
  85.  * (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall.     *
  86.  * All Rights Reserved.                                                   *
  87.  *                                                                        *
  88.  * DISCLAIMER: The authors and publisher of this book have used their     *
  89.  * best efforts in preparing the book. These efforts include the          *
  90.  * development, research, and testing of the theories and programs        *
  91.  * to determine their effectiveness. The authors and publisher make       *
  92.  * no warranty of any kind, expressed or implied, with regard to these    *
  93.  * programs or to the documentation contained in these books. The authors *
  94.  * and publisher shall not be liable in any event for incidental or       *
  95.  * consequential damages in connection with, or arising out of, the       *
  96.  * furnishing, performance, or use of these programs.                     *
  97.  *************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement