Daisypath Friendship tickers

Minggu, 26 Februari 2012

Contoh Program Multiple Selection List di Java

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:
contoh-program-multiple-list-java

Berikut ini programnya:
01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04 
05public 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"
11        };
12 
13    public MultipleSelectionTest() {
14        super ("Multiple Selection List");     
15 
16        Container container = getContentPane();
17        container.setLayout(new FlowLayout());
18 
19        lstColor = new JList (arrColorName);
20        lstColor.setVisibleRowCount(5);
21        lstColor.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
22        container.add(new JScrollPane (lstColor));
23 
24        btnCopy = new JButton ("Copy >>>");       
25 
26        btnCopy.addActionListener(
27            new ActionListener() {
28                public void actionPerformed (ActionEvent e) {
29                    lstCopy.setListData(lstColor.getSelectedValues());
30                }
31            } //end of class
32        );
33 
34        container.add(btnCopy);    
35 
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))    
42 
43        setSize (400,300);
44        setLocationRelativeTo(null);
45        setVisible(true);
46    }
47 
48    public static void main (String args[]) {
49        MultipleSelectionTest test = new MultipleSelectionTest();
50        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51    }
52 
53}
maju terus ilmu pengetahuan Indonesia!

Tidak ada komentar:

Posting Komentar