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