List data events occur when the contents of a mutable list change. Since the model — not the component — fires these events, you have to register a list data listener with the list model. If you have not explicitly created a list with a mutable list model, then your list is immutable, and its model will not fire these events.
The following example demonstrates list data events on a mutable list:
Try this:intervalAdded
event was fired.intervalRemoved
event was fired.contentsChanged
events are fired â one for the item that moved and one for the item that was displaced.You can find the demo's code in ListDataEventDemo.java
. Here is the code that registers a list data listener on the list model and implements the listener:
//...where member variables are declared... private DefaultListModel listModel; ... //Create and populate the list model listModel = new DefaultListModel(); ... listModel.addListDataListener(new MyListDataListener()); class MyListDataListener implements ListDataListener { public void contentsChanged(ListDataEvent e) { log.append("contentsChanged: " + e.getIndex0() + ", " + e.getIndex1() + newline); } public void intervalAdded(ListDataEvent e) { log.append("intervalAdded: " + e.getIndex0() + ", " + e.getIndex1() + newline); } public void intervalRemoved(ListDataEvent e) { log.append("intervalRemoved: " + e.getIndex0() + ", " + e.getIndex1() + newline); } }The List Data Listener API
The ListDataListener Interface
ListDataListener
has no corresponding adapter class.
java.util.EventObject
) Return the object that fired the event. int getIndex0() Return the index of the first item whose value has changed. int getIndex1() Return the index of the last item whose value has changed. int getType() Return the event type. The possible values are: CONTENTS_CHANGED
, INTERVAL_ADDED
, or INTERVAL_REMOVED
. Examples that Use List Data Listeners
The following table lists the examples that use list data listeners.
Example Where Described NotesListDataEventDemo
This section Reports all list data events that occur on a list.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4