Java Reference
In-Depth Information
Lists can be created and filled with the contents of an array or a vector. The following
constructors are available:
JList() —Create an empty list.
n
JList( Object[] ) —Create a list that contains an array of the specified class (such
as String ).
n
JList( Vector ) —Create a list that contains the specified java.util.Vector
object.
n
An empty list can be filled by calling its setListData() method with either an array or
vector as the only argument.
Unlike combo boxes, lists display more than one of their rows when they are presented
in a user interface. The default is to display eight items. To change this, call
setVisibleRowCount( int ) with the number of items to display.
The getSelectedValues() method returns an array of objects containing all the items
selected in the list.
The Subscriptions application in Listing 9.8 displays eight items from an array of
strings.
LISTING 9.8
The Full Text of Subscriptions.java
1: import javax.swing.*;
2:
3: public class Subscriptions extends JFrame {
4: String[] subs = { “0xDECAFBAD”, “Cafe au Lait”,
5: “Hack the Planet”, “Ideoplex”, “Inessential”, “Intertwingly”,
6: “Markpasc”, “Postneo”, “RC3”, “Workbench” };
7: JList subList = new JList(subs);
8:
9: public Subscriptions() {
10: super(“Subscriptions”);
11: setSize(150, 300);
12: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13: JPanel panel = new JPanel();
14: JLabel subLabel = new JLabel(“RSS Subscriptions:”);
15: panel.add(subLabel);
16: subList.setVisibleRowCount(8);
17: JScrollPane scroller = new JScrollPane(subList);
18: panel.add(scroller);
19: add(panel);
20: setVisible(true);
21: }
22:
Search WWH ::




Custom Search