O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Representing List Data

Swing uses one interface and two classes to maintain a model of the list elements. When programming with lists, you will often find that you can reuse these classes without modification. Occasionally, you may find it necessary to extend or even rewrite these classes to obtain functionality that is lacking. In either case, it’s important to examine all three in detail. Let’s start with the easiest: ListModel.

The ListModel Interface

ListModel is a simple interface for accessing the data of the list. It has four methods: one method to retrieve data in the list, one method to obtain the total size of the list, and two methods to register and unregister change listeners on the list data. Note that the ListModel interface itself only contains a method for retrieving the list elements — not setting them. Methods to set list values are defined in classes that implement this interface.

Properties

The ListModel interface defines two properties, shown in Table 7.1. elementAt is an indexed property that lets you retrieve individual objects from the list; size tells you the total number of elements.

Table 7-1. ListModel Properties

Property

Data Type

get

is

set

bound

Default Value

elementAt (indexed)

Object

•

    

size

int

•

    

Events

The ListModel interface also contains the standard addListDataListener() and removeListDataListener() event subscription methods. These methods accept listeners that wish to be notified when the contents of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required