12.12 JList
A list displays a series of items from which the user may select one or more items (see the output of Fig. 12.24). Lists are created with class JList, which directly extends class JComponent. Class JList—which like JComboBox is a generic class—supports single-selection lists (which allow only one item to be selected at a time) and multiple-selection lists (which allow any number of items to be selected). In this section, we discuss single-selection lists.
The application of Figs. 12.23–12.24 creates a JList containing 13 color names. When a color name is clicked in the JList, a ListSelectionEvent occurs and the application changes the background color of the application window to the selected color. Class ListTest (Fig. 12.24) contains ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access