15.2. JList

A JList presents a list of choices for selection. Either a single item or multiple items can be selected. In the following subsections, we present four approaches for creating a JList. The first approach creates a fixed set of choices by simply passing data directly to the JList. The second approach takes advantage of the default list model to support changeable choices. The third approach demonstrates a custom list model (data structure) for holding the list items. Finally, the fourth approach uses a custom renderer to build a custom JComponent for each item in the list.

JList with a Fixed Set of Choices

The simplest way to create a JList is to supply an array of strings to the JList constructor. Unlike an AWT List, a JList does ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.