Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click to go to the Oscars.
Click to go to the Oscars.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


ListSelectionListener and ListSelectionEvent

To monitor changes in a class that implements the ListSelectionModel interface, the ListSelectionListener interface must be implemented. This interface and the ListSelectionEvent class are shown in Listing 10.10. When the list selection model changes, the valueChanged method of the listener is called. The parameter to this method is an instance of the ListSelectionEvent class. This event delivers the range of where a selection change has occurred in the selection model. However, the event does not tell the listener which elements have changed. It is the responsibility of the listener to query the model to determine the new selection state of each element in the range contained in the ListSelectionEvent.

The other piece of information contained in a ListSelectionEvent is the valueIsAdjusting flag. When this is true, it tells the listener that there are many events being delivered in rapid succession. If the listener is a graphical component, it may choose not to repaint itself until this flag is false. At that time, the rapid series of events will have ended.

Listing 10.10 The ListSelectionListener Interface and ListSelectionEvent Class

public interface ListSelectionListener extends EventListener
{
  /**
   * Called whenever the value of the selection changes.
   * @param e the event that characterizes the change.
   */
  void valueChanged(ListSelectionEvent e);
}

public class ListSelectionEvent extends EventObject
{
    private int firstIndex;
    private int lastIndex;
    private boolean isAdjusting;

    /**
     * Represents a change in selection status between firstIndex
     * and lastIndex inclusive (firstIndex is less than or equal to
     * lastIndex).  At least one of the rows within the range will
     * have changed, a good ListSelectionModel implementation will
     * keep the range as small as possible.
     *
     * @param firstIndex The first index that changed.
     * @param lastIndex The last index that changed,
     *                  lastIndex >= firstIndex.
     * @param isAdjusting An indication that this is one of
     *                    a rapid series of events
     */
    public ListSelectionEvent(Object source, int firstIndex,
                              int lastIndex, boolean isAdjusting)
{
    super(source);
    this.firstIndex = firstIndex;
    this.lastIndex = lastIndex;
    this.isAdjusting = isAdjusting;
    }

    /**
     * @return The first row whose selection value may have changed.
     */
    public int getFirstIndex() { return firstIndex; }

    /**
     * @return The last row whose selection value may have changed.
     */
    public int getLastIndex() { return lastIndex; }

    /**
     * @return True if this is one of a rapid series of events
     */
    public boolean getValueIsAdjusting() { return isAdjusting; }
}

JList Class and Model Interactions

In the previous sections, you saw the models that the JList view interacts with. The view-to-model interaction is depicted in Figure 10.6. Arrow 1 is the view initializing from the ListModel. The view adds a listener to the data model so that it will be notified if the model changes. When the user interacts with the view, the selection is changed in the ListSelectionModel. This is shown by arrow 2. The view also listens to the model for selection changes. This allows the selection to be made programmatically and the view to be updated. If the data in the model changes, a change event will be sent to the view, as shown by arrow 3.


Figure 10.6  JList view and model interaction.

Combo Boxes

A combo box is a visual component similar to a list. It allows a single element in a list to be selected. The selected element is shown in the combo box, as well as a control to expand the combo box into a list. An unexpanded combo box requires less screen real estate than a list. However, the box must be expanded to show the possible choices in the combo box.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.