![]() ![]() ![]() |
11
Multi-Select List
Features of JCMultiSelectList
Properties
Constructors and Methods
Examples
11.1 Features of JCMultiSelectList
JCMultiSelectList
matches the API forJList
except that two lists instead of one appear in the component's GUI. There are four buttons between the two lists that move items back and forth. The left-hand list contains non-selected items and the right-hand list contains the selected items. In the context of aJCMultiSelectList
, if an item is marked as selected, it means more than simply being highlighted. Besides providing a visual division of list items into the two columns, selected and non-selected, there are numerous methods for dealing with the values and indices of a set of selected values.
JCMultiSelectList
provides a visual component that clearly distinguishes items chosen from a given list by removing them from the original list and placing them in another container. See the next figure for details.- You can create a
JCMultiSelectList
using one of its five constructors, four of which correspond to the constructors of aJList
. The remaining constructor has an emptyListModel
, but has a parameter for setting the horizontal gap between the two lists.- As with a
JList
, you can specify content using theListModel
interface, or you can supply content usingObject
s orVector
s.- You are able to modify content in various ways depending on which objects you used to populate the main list.
- End users may perform single or multiple, contiguous or non-contiguous selections of list items.
- The
ListSelectionModel
generates aListSelectionEvent
to allow you to process user interactions.Four buttons control the movement of items in one list to the other. They are shown in the next figure. The top button moves selected items from the left-hand list to the right-hand list. The second from the top moves all items out of the left-hand list to the right-hand list. The bottom two buttons perform the analogous operation, but in the other direction.
Figure 41 : GUI for JCMultiSelectList.
You set a
ListModel
on the component, or you can use the default model that is provided. In the latter case, you simply addObject
s to the existing component. The component usesgetSelected()
to determine which items should appear in the right-hand list. Only the non-selected items show on the left.
11.2 Properties
A selection of
JCMultiSelectList
's properties are shown in the following table. Please see Properties of JCMultiSelectList and Bean Properties Reference in Appendix A for a complete list.
Property
Description
Sets the prototypical cell value, a cell used for the calculation of cell widths, rather than forcing the calculation to inspect every item in the list.
11.3 Constructors and Methods
Constructors
There are constructors for
ListModel
, array, andVector
types of data models.Methods
JCMultiSelectList
subclasses fromJComponent
, giving it a host of inherited methods. It overrides some, likeaddListSelectionListener
, to provide specific functionality. The table shows a few frequently used methods. Please refer to the API for a full list.
Method Name
Description
Adds a listener to the
ListSelectionListener
's list. Its parameters are ajavax.swing.event
and aListSelectionListener
.Adds the specified interval to the current selection. It takes two
int
parameters that specify the beginning and ending positions of the interval.Moves the items selected in the right list to the left list.
Forwards the given notification event to all registered listeners. It takes two
int
parameters that specify the begin and end positions of the interval, and a thirdboolean
parameter that specifies whether this is one of a rapidly occurring series of events. Parameters areint firstIndex, int lastIndex, boolean isAdjusting
Returns the first index argument from the most recent interval selection.
Sets the list's data model. Its single parameter is a
javax.swing.ListModel
.
11.4 Examples
See
examples.elements.MultiSelectList
for a full listing of this example.One of
static String[] data = {"Tom", "Dick", "Harry"};JCMultiSelectList
's constructors takes an array of list items as its parameter. Call this arraydata
and define it as follows:Create a
JCMultiSelectList list = new JCMultiSelectList(data);JCMultiSelectList
and give it the data:You process events generated by the list by implementing the
valueChanged()
method of theListSelectionListener
interface.
![]() ![]() ![]() |