6.1.1. The JList Component

The JList component shows a number of items inside a single box. Figure 6.1 shows an admittedly silly example. The user can select the attributes for the fox, such as “quick,” “brown,” “hungry,” “wild,” and, because we ran out of attributes, “static,” “private,” and “final.” You can thus have the private static final fox jump over the lazy dog.

Image

Figure 6.1. A list box

As of Java SE 7, JList is a generic type. The type parameter is the type of the values the user can select. In this example, we use a JList<String>.

To construct this list component, start out with an array of strings and pass that array to the JList constructor: ...

Get Core Java® Volume II—Advanced Features, Ninth Edition now with O’Reilly online learning.

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