MultipleSelectionTest.java
// Fig. 12.26: MultipleSelectionTest.java // JList that allows multiple selections. import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; class MultipleSelectionFrame extends JFrame { private final JList<String> colorJList; // list to hold color names private final JList<String> copyJList; // list to hold copied names private JButton copyJButton; // button to copy selected names private static final String[] colorNames = {"Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow"}; // MultipleSelectionFrame constructor public MultipleSelectionFrame() { super("Multiple Selection Lists"); setLayout(new FlowLayout()); colorJList = new JList<String>(colorNames); // list of color names colorJList.setVisibleRowCount(5); // show five rows colorJList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); add(new JScrollPane(colorJList)); // add list with scrollpane copyJButton = new JButton("Copy >>>"); copyJButton.addActionListener( new ActionListener() // anonymous inner class { // handle button event @Override public void actionPerformed(ActionEvent event) { // place selected values in copyJList copyJList.setListData( colorJList.getSelectedValuesList().toArray( new String[0])); } } ); add(copyJButton); // add copy button to JFrame copyJList = new JList<String>(); // list to hold copied color names copyJList.setVisibleRowCount(5); // show 5 rows copyJList.setFixedCellWidth(100); // set width copyJList.setFixedCellHeight(15); // set height copyJList.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION); add(new JScrollPane(copyJList)); // add list with scrollpane } } // end class MultipleSelectionFrame public class MultipleSelectionTest { public static void main(String[] args) { MultipleSelectionFrame multipleSelectionFrame = new MultipleSelectionFrame(); multipleSelectionFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); multipleSelectionFrame.setSize(350, 150); multipleSelectionFrame.setVisible(true); } } // end class MultipleSelectionTest
Maintained by John Loomis, updated Sat Feb 11 17:21:17 2017