COntoh program java berikut ini merupakan variasi dari contoh program list sebelumnya. Di dalam program ini, list dibuat agar bisa dipilih lebih dari satu pilihan.
Berikut ini tampilan programnya:
Berikut ini programnya:
02 | import java.awt.event.*; |
05 | public class MultipleSelectionTest extends JFrame { |
06 | private JList lstColor, lstCopy; |
07 | private JButton btnCopy; |
08 | private final String arrColorName[] = |
09 | { "Black" , "Blue" , "Cyan" , "Dark Gray" , "Gray" , "Green" , "Light Gray" , |
10 | "Magenta" , "Orange" , "Pink" , "Red" , "Yellow" , "White" |
13 | public MultipleSelectionTest() { |
14 | super ( "Multiple Selection List" ); |
16 | Container container = getContentPane(); |
17 | container.setLayout( new FlowLayout()); |
19 | lstColor = new JList (arrColorName); |
20 | lstColor.setVisibleRowCount( 5 ); |
21 | lstColor.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
22 | container.add( new JScrollPane (lstColor)); |
24 | btnCopy = new JButton ( "Copy >>>" ); |
26 | btnCopy.addActionListener( |
27 | new ActionListener() { |
28 | public void actionPerformed (ActionEvent e) { |
29 | lstCopy.setListData(lstColor.getSelectedValues()); |
34 | container.add(btnCopy); |
36 | lstCopy = new JList(); |
37 | lstCopy.setVisibleRowCount( 5 ); |
38 | lstCopy.setFixedCellHeight( 15 ); |
39 | lstCopy.setFixedCellWidth( 100 ); |
40 | lstCopy.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); |
41 | container.add( new JScrollPane(lstCopy)) |
44 | setLocationRelativeTo( null ); |
48 | public static void main (String args[]) { |
49 | MultipleSelectionTest test = new MultipleSelectionTest(); |
50 | test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
maju terus ilmu pengetahuan Indonesia!
Tidak ada komentar:
Posting Komentar