Work with JList

The last component that we will examine is JList. This is Swing’s basic list class. It supports the selection of one or more items from a list. Although often the list consists of strings, it is possible to create a list of just about any object that can be displayed. JList is so widely used in Java that it is highly unlikely that you have not seen one before.

In the past, the items in a JList were represented as Object references. However, with the release of JDK 7, JList was made generic, and it is now declared like this:

class JList<E>

Here, E represents the type of the items in the list. As a result, JList is now type-safe.

NOTE

The discussion and example that follow use the generic version of JList. If your compiler predates ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition 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.