JList Class

Package: javax.swing

Using this creates a list component, which displays lists of objects within a box. Depending on how the list is configured, the user can be allowed to select one item in the list or multiple items. In addition, you have amazing control of how the items in the list are displayed.

CrossRef.eps Lists are almost always used in conjunction with scroll panes to allow the user to scroll the contents of the list. For more information, see JScrollPane Class.

Figure 5-8 shows a list component.

9781118239742-fg0508.tif

Figure 5-8

Constructors

Constructor

Description

JList()

Creates an empty list

JList(ListModel list)

Creates a list that uses the specified list model

JList(Object[] items)

Creates a list and fills it with the values in the array

JList(Vector[] items)

Creates a list and fills it with the values in the vector

void clearSelection()

Clears all selections

Methods

Method

Description

int getSelectedIndex()

Returns the index of the first selected item, or –1 if no items are selected.

int[] getSelected Indexes()

Returns an array with the index of each selected item. The array is empty if no items are selected.

Object getSelected Value()

Returns the first selected item or null if no items are selected.

Object[] getSelected ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.