Java Reference
In-Depth Information
Figure 13.11
Double-clicking an item in the list displays it in the label.
An AWT list manages its own scroll bars; however, when using the Swing
list component JList, you need to use the JScrollPane class to create scroll
bars. The process is similar to using JScrollPane for the JTextArea
component. See the earlier sidebar Working with Scroll Panes for an
example of how to use the JScrollPane class.
Swing Lists
The javax.swing.JList class represents a Swing list. The JList class has four
constructors:
public JList(). Creates a new, empty single-selection list.
public List(Object [] listData).
Creates a list with the given array of
objects as its initial data.
public JList(Vector listData).
Creates a list with the elements of the Vec-
tor as its initial data.
public JList(ListModel listData). Creates a list with the given ListModel.
A ListModel object uses a Vector to maintain the elements of the list.
I want to make a couple of comparisons between List and JList:
When using a List, items are added to the list one at a time using the
add() methods. With a JList, items are added from a data structure
such as a Vector or an array using either a constructor or one of the
setListData() methods. (There are no add() methods in JList.)
■■
A List generates an ItemEvent, while a JList generates a ListSelection-
Event when the selected item or items change.
■■
A List generates an ActionEvent when an item is double-clicked, while
a JList does not generate any action events.
■■
Search WWH ::




Custom Search