The JList Class

The JList class is the generic Swing implementation of a list component. The class works reasonably well as a drop-in replacement for the AWT List class. If the selection mode is set appropriately, you can make multiple selections by clicking with the mouse while holding down the “Shift” or “Ctrl” modifier keys. Unlike AWT, the JList class does not provide scrolling capabilities; it must be set as the viewport of a JScrollPane in order to support scrolling. Figure 7.8 shows the JList component with three separate look-and-feels.

The JList component in the three look-and-feels

Figure 7-8. The JList component in the three look-and-feels

Properties

The JList class essentially combines the features of the data model, the selection model, and the cell renderer into a single Swing component. The properties of the JList class are shown in Table 7.9.

Table 7-9. JList Properties

Property

Data Type

get

is

set

bound

Default

See also properties from JComponent (Table 3.5)       

model

ListModel

•

 

•

•

selectionModel

ListSelectionModel

•

 

•

•

DefaultListSelectionModel()

UI

ListUI

•

 

•

•

from L&F

UIClassID*

String

•

   

"ListUI"

accessibleContext*

AccessibleContext

•

   

JList.AccessibleJList()

anchorSelectionIndex

int

•

   

cellRenderer

ListCellRenderer

•

 

•

•

from L&F

firstVisibleIndex ...

Get Java Swing 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.