Class 2 |
Intermediate Java 30-IT-397 |
|
The JList Component
-
Items are placed inside the JList.
-
You click on an item itself to select it.
-
You can permit multiple selection, or specify single selection.
-
You can make a simple JList by passing in a String array to the JList constructor.
Example:
String[] arrBuildings = {"Sander", "French", "Edwards", "Rievischl",
"McMicken"};
JList lstBuildings = new JList(arrBuildings);
-
Interestingly, the JList will not scroll automatically. To make it
scroll, you have to put it in a JScrollPane. Perhaps the easiest
way to do this is via the JScrollPane's constructor. Then, you can
add the JScrollPane (not the JList) to the panel you are using for display
(either a JFrame object or JPanel object). Example:
JScrollPane jspBuildings = new JScrollPane(lstBuildings);
myFrame.add(jspBuildings);
-
You can change the default items shown from 8 to any number of your choice
via JList's setVisibleRowCount(int rowCount) method.
-
By default, users can choose multiple items from the JList. You can
change the selection mode (multiple selection vs. single selection) via
the setSelectionMode(int selectionMode) method. You can pass in the
static final ListSelectionModel.SINGLE_SELECTION for single selection.
ListSelectionModel.SINGLE_INTERVAL_SELECTION allows the user to choose
one range of items.
Using Events

Created by: Brandan Jones
December 17, 2001