Chapter 7. Lists and Combo Boxes

This chapter deals with two similar components: lists and combo boxes. Both components present a catalog of choices to the user. A list allows the user to make single or multiple selections. A combo box permits only a single selection, but can be combined with a text field that allows the user to type in a selection as well. From a design standpoint, both lists and combo boxes share similar characteristics, and as you will soon see, both can be extended in ways that many Swing components cannot.


A list is a graphical component from which the user can select choices. Lists typically display several items at a time, allowing the user to make either a single selection or multiple selections. In the event that the inventory of the list exceeds the component’s display, the list is often coupled with a scrollbar to navigate through the entire contents.

AWT limited the contents of its List component to strings. The Swing JList component lifts this restriction. List elements can now be strings, images — any Java component capable of painting itself. Swing offers a wide degree of flexibility with list components; they can be as simple or as complex as the programmer’s needs dictate.

A simple Swing list

Figure 7-1. A simple Swing list

Let’s get our feet wet with a simple list. The following example uses the Swing list class, JList, to create a single-selection list composed only ...

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.