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 ...