Java Reference
In-Depth Information
public void clear()
public Object remove(int index)
public void removeAllElements()
public boolean removeElement(Object element)
public void removeElementAt(int index)
public void removeRange(int fromIndex, int toIndex)
The removeElement() method returns a status: true if it found the object and removed it,
and false otherwise.
The DefaultListModel class is useful when you don't have your data in an existing data
structure. For example, the results of a database query come back as a JDBC ResultSet . If you
wish to use those results as the basis for what to display in a JList , you must store them some-
where. That somewhere can be a DefaultListModel , as demonstrated by the following:
ResultSet results = aJDBCStatement.executeQuery(
"SELECT columnName FROM tableName");
DefaultListModel model = new DefaultListModel();
while (results.next()) {
model.addElement(result.getString(1));
}
Listening for ListModel Events with a ListDataListener
If you're interested in finding out when the contents of the list model change, you can register
a ListDataListener with the model. Three separate methods of the interface tell you when
contents are added, removed, or altered. Altering the data model means adding and/or removing
contents from one or more regions of the data model or changing the existing contents without
adding or removing anything. The following is the interface definition:
public interface ListDataListener extends EventListener {
public void contentsChanged(ListDataEvent e);
public void intervalAdded(ListDataEvent e);
public void intervalRemoved(ListDataEvent e);
}
Upon notification of the list-altering event, you're passed a ListDataEvent instance, which
contains three properties, as shown in Table 13-2.
Table 13-2. ListDataEvent Properties
Property Name
Data Type
Access
index0
int
Read-only
index1
int
Read-only
type
int
Read-only
 
Search WWH ::




Custom Search