Java Reference
In-Depth Information
Manually Selecting JList Events
In addition to detecting when a user selects items in a list, you can also programmatically select
or deselect items. If any ListSelectionListener objects are attached to the JList , they will also
be notified when the set of selected items is programmatically altered. The following methods
are available:
For a single item, public void setSelectedValue(Object element, boolean shouldScroll)
selects the first item that matches the element . If the element wasn't previously selected,
everything that was selected will be deselected first.
For a range of items, public void setSelectedInterval(int index0, int index1)
selects an inclusive range.
For adding a range of selected items to the already selected set, use public void
addSelectedInterval(int index0, int index1) .
You can clear all the selected items with the public void clearSelection() method.
You can clear a range of selected items with the public void removeSelectedInterval
(int index0, int index1) method.
Displaying Multiple Columns
Typically, whenever you work with a JList , you present its choices within a single column.
While this is the usual manner of usage, the Swing JList control offers support for displaying
its choices within multiple columns. Through the help of the setLayoutOrientation() method,
you can set each JList orientation to lay out cells in columns horizontally or vertically.
JList.VERTICAL is the default setting where everything appears in one column.
To lay out cells horizontally, before going to next row, use the value JList.HORIZONTAL_WRAP .
For example, a list with nine elements would be displayed as shown here:
0
1
2
3
4
5
6
7
8
To lay out cells vertically, before going to next column, use the value JList.VERTICAL_WRAP .
For example, a list with nine elements would be displayed as shown here:
0
3
6
1
4
7
2
5
8
Set the visibleRowCount property of JList to control the number of rows. Otherwise, the
list width determines the row count for HORIZONTAL_WRAP and the list height for VERTICAL_WRAP .
Search WWH ::




Custom Search