How to Write a List Data Listener

 < Day Day Up > 

List data events occur when the contents of a mutable list change. Since the modelnot the componentfires these events, you have to register a list data listener with the list model. If you haven't explicitly created a list with a mutable list model, then your list is immutable and its model will not fire these events.

Note: Combo box models also fire list data events. However, you normally don't need to know about them unless you're creating a custom combo box model.


Figure 8 demonstrates list data events on a mutable list:

Figure 8. The ListDataEventDemo application.

graphics/10fig08.gif

Try This:

  1. graphics/cd_icon.gif

    Run ListDataEventDemo using Java Web Start or compile and run the example yourself. [18]

    [18] To run ListDataEventDemo using Java Web Start, click the ListDataEventDemo link on the RunExamples/events.html page on the CD. You can find the source files here: JavaTutorial/uiswing/events/example-1dot4/index.html#ListDataEventDemo .

  2. Type in the name of your favorite ski resort and click the Add button. An intervalAdded event is fired .

  3. Select a few continguous items in the list and click the Delete button. An intervalRemoved event is fired.

  4. Select one item and move it up or down in the list with the arrow buttons . Two contentsChanged events are firedone for the item that moved and one for the item that was displaced.

You can find the demo's code in ListDataEventDemo.java . Here's 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

Table 20 lists the methods in the ListDataListener interface and Table 21 describes the methods in the ListDataEvent class. Note that ListDataListener has no corresponding adapter class. Also refer to the ListDataListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/ListDataListener.html. The API documentation for ListDataEvent is online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/ListDataEvent.html.

Table 20. The ListDataListener Interface

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 contents of one or more items in the list have changed.

Table 21. The ListDataEvent API

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 .

Examples That Use List Data Listeners

The following examples use list data listeners.

Example

Where Described

Notes

ListDataEventDemo

This section

Reports all list data events that occur on a list.

 < Day Day Up > 


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net