The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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.
Combo box models also fire list data events. However, you normally do not need to know about them unless you are creating a custom combo box model.
The following example demonstrates list data events on a mutable list:
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 ListDataListener Interface
ListDataListener
has no corresponding adapter class.
Method | Purpose |
---|---|
intervalAdded(ListDataEvent) | Called when one or more items have been added to the list. |
intervalRemoved(ListDataEvent) | Called when one or more items have been removed from the list. |
contentsChanged(ListDataEvent) | Called when the contents of one or more items in the list have changed. |
Method | Purpose |
---|---|
Object getSource() (in 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 . |
The following table lists the examples that use list data listeners.
Example | Where Described | Notes |
---|---|---|
ListDataEventDemo |
This section | Reports all list data events that occur on a list. |