Java Reference
In-Depth Information
Some of the methods in JList include:
public void addListSelectionListener(ListSelectionListener s). Gener-
ates a ListSelectionEvent each time the selected list of items changes.
public void setListData(Object [] listData).
Changes the items in the list
to the elements in the array.
public void setListData(Vector listData).
Changes the items in the list to
the elements in the Vector.
public Object getSelectedValue().
Returns the currently selected item of
the list.
public Object [] getSelectedValues(). Returns the currently selected
items. Use this method when the list allows multiple selection.
public void setSelectionMode(int mode). Sets the selection mode of this
list. The possible values are SINGLE_SELECTION, SINGLE_INTER-
VAL_SELECTION, MULTIPLE_INTERVAL_SELECTION, fields in the
ListSelectionModel interface.
I want to demonstrate how to use JList because lists are a common compo-
nent. Let's take a look at combo boxes first, then I will show you an example
(the upcoming SelectionDialog program) that demonstrates using both lists
and combo boxes.
Combo Boxes
A combo box is a drop-down list that only displays the currently selected item.
The java.awt.Choice class represents an AWT combo box, and the
javax.swing.JComboBox class represents a Swing combo box. Let's take a look
at both classes.
AWT Choice
The Choice class is similar to the List class. (A combo box is basically a single-
selection list that only displays one item.) The Choice class has one constructor:
public Choice().
Creates a new, empty combo box.
Items are added to a Choice using the add() method, which takes in a String.
The getSelectedItem() method returns the currently selected item. When a
selection is made, the Choice generates an ItemEvent containing the currently
selected item.
Search WWH ::




Custom Search