javax.swing

Interface ListSelectionModel

Known Implementing Classes:
DefaultListSelectionModel

public interface ListSelectionModel

A model that tracks the selection status of a list of items. Each item in the list is identified by a zero-based index only, so the model can be used to track the selection status of any type of list. The model supports three modes: The model uses an event notification mechanism to notify listeners (see ListSelectionListener) about updates to the selection model.

This model is used to track row selections in the JList component, and row and column selections in the JTable component.

Field Summary

static int
MULTIPLE_INTERVAL_SELECTION
A selection mode in which any combination of items can be selected.
static int
SINGLE_INTERVAL_SELECTION
A selection mode in which a single interval can be selected (an interval is a range containing one or more contiguous items).
static int
SINGLE_SELECTION
A selection mode in which only one item can be selected.

Method Summary

void
addListSelectionListener(ListSelectionListener listener)
Registers a listener with the model so that it receives notification of changes to the model.
void
addSelectionInterval(int anchor, int lead)
Marks the items in the specified interval as selected.
void
clearSelection()
Clears the current selection from the model.
int
getAnchorSelectionIndex()
Returns the index of the anchor item.
int
getLeadSelectionIndex()
Returns the index of the lead item.
int
getMaxSelectionIndex()
Returns the highest selected index, or -1 if there is no selection.
int
getMinSelectionIndex()
Returns the lowest selected index, or -1 if there is no selection.
int
getSelectionMode()
Returns the selection mode, which is one of SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION and MULTIPLE_INTERVAL_SELECTION.
boolean
getValueIsAdjusting()
Returns a flag that is passed to registered listeners when changes are made to the model.
void
insertIndexInterval(int index, int length, boolean before)
Inserts a new interval containing length items at the specified index (the before flag indicates whether the range is inserted before or after the existing item at index).
boolean
isSelectedIndex(int index)
Returns true if the specified item is selected, and false otherwise.
boolean
isSelectionEmpty()
Returns true if there is no selection, and false otherwise.
void
removeIndexInterval(int index0, int index1)
Removes the items in the specified range (inclusive) from the selection model.
void
removeListSelectionListener(ListSelectionListener listener)
Deregisters a listener so that it no longer receives notification of changes to the model.
void
removeSelectionInterval(int anchor, int lead)
Marks the items in the specified interval as not selected.
void
setAnchorSelectionIndex(int index)
Sets the index of the anchor item.
void
setLeadSelectionIndex(int index)
Sets the index of the lead item.
void
setSelectionInterval(int anchor, int lead)
Sets the selection interval to the specified range (note that anchor can be less than, equal to, or greater than lead).
void
setSelectionMode(int mode)
Sets the selection mode.
void
setValueIsAdjusting(boolean valueIsAdjusting)
Sets the flag that is passed to listeners for each change notification.

Field Details

MULTIPLE_INTERVAL_SELECTION

public static final int MULTIPLE_INTERVAL_SELECTION
A selection mode in which any combination of items can be selected.
Field Value:
2

SINGLE_INTERVAL_SELECTION

public static final int SINGLE_INTERVAL_SELECTION
A selection mode in which a single interval can be selected (an interval is a range containing one or more contiguous items).
Field Value:
1

SINGLE_SELECTION

public static final int SINGLE_SELECTION
A selection mode in which only one item can be selected.
Field Value:
0

Method Details

addListSelectionListener

public void addListSelectionListener(ListSelectionListener listener)
Registers a listener with the model so that it receives notification of changes to the model.
Parameters:
listener - the listener (null ignored).

addSelectionInterval

public void addSelectionInterval(int anchor,
                                 int lead)
Marks the items in the specified interval as selected. The behaviour of this method depends on the selection mode:
  • SINGLE_SELECTION - only the lead item is selected;
  • SINGLE_INTERVAL_SELECTION - the existing selection interval is replaced by the specified interval;
  • MULTIPLE_INTERVAL_SELECTION - the specified interval is merged into the currently selected intervals.
Note that anchor can be less than, equal to, or greater than lead.
Parameters:
anchor - the index of the anchor item
lead - the index of the lead item.

clearSelection

public void clearSelection()
Clears the current selection from the model. If the selection state changes (that is, the existing selection is non-empty) a ListSelectionEvent should be sent to all registered listeners.

FIXME: what happens to the anchor and lead selection indices (the spec is silent about this)? See:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4334792


getAnchorSelectionIndex

public int getAnchorSelectionIndex()
Returns the index of the anchor item.
Returns:
The index of the anchor item.

getLeadSelectionIndex

public int getLeadSelectionIndex()
Returns the index of the lead item.
Returns:
The index of the lead item.

getMaxSelectionIndex

public int getMaxSelectionIndex()
Returns the highest selected index, or -1 if there is no selection.
Returns:
The highest selected index.

getMinSelectionIndex

public int getMinSelectionIndex()
Returns the lowest selected index, or -1 if there is no selection.
Returns:
The lowest selected index.

getSelectionMode

public int getSelectionMode()
Returns the selection mode, which is one of SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION and MULTIPLE_INTERVAL_SELECTION.
Returns:
The selection mode.

getValueIsAdjusting

public boolean getValueIsAdjusting()
Returns a flag that is passed to registered listeners when changes are made to the model. See the description for setValueIsAdjusting(boolean) for more information.
Returns:
The flag.

insertIndexInterval

public void insertIndexInterval(int index,
                                int length,
                                boolean before)
Inserts a new interval containing length items at the specified index (the before flag indicates whether the range is inserted before or after the existing item at index). FIXME: What is the selection status of the new items? Bug 4870694. FIXME: What event is generated?
Parameters:
index - the index of the item.
length - the number of items in the interval to be inserted.
before - if true, the interval should be inserted before index, otherwise it is inserted after.

isSelectedIndex

public boolean isSelectedIndex(int index)
Returns true if the specified item is selected, and false otherwise. Special note: if index is negative, this method should return false (no exception should be thrown).
Parameters:
index - the item index (zero-based).
Returns:
true if the specified item is selected, and false otherwise.

isSelectionEmpty

public boolean isSelectionEmpty()
Returns true if there is no selection, and false otherwise.
Returns:
true if there is no selection, and false otherwise.

removeIndexInterval

public void removeIndexInterval(int index0,
                                int index1)
Removes the items in the specified range (inclusive) from the selection model. This method should be called when an interval is deleted from the underlying list. FIXME: what happens to the lead and anchor indices if they are part of the range that is removed? FIXME: what event is generated
Parameters:
index0 - XXX
index1 - XXX

removeListSelectionListener

public void removeListSelectionListener(ListSelectionListener listener)
Deregisters a listener so that it no longer receives notification of changes to the model. If the specified listener is not registered with the model, or is null, this method does nothing.
Parameters:
listener - the listener (null ignored).

removeSelectionInterval

public void removeSelectionInterval(int anchor,
                                    int lead)
Marks the items in the specified interval as not selected. The behaviour of this method depends on the selection mode:
  • SINGLE_SELECTION - XXX;
  • SINGLE_INTERVAL_SELECTION - XXX;
  • MULTIPLE_INTERVAL_SELECTION - XXX.
Note that anchor can be less than, equal to, or greater than lead.
Parameters:
anchor - the index of the anchor item
lead - the index of the lead item.

setAnchorSelectionIndex

public void setAnchorSelectionIndex(int index)
Sets the index of the anchor item.
Parameters:
index - the item index.

setLeadSelectionIndex

public void setLeadSelectionIndex(int index)
Sets the index of the lead item.
Parameters:
index - the item index.

setSelectionInterval

public void setSelectionInterval(int anchor,
                                 int lead)
Sets the selection interval to the specified range (note that anchor can be less than, equal to, or greater than lead). If this results in the selection being changed, a ListSelectionEvent is sent to all registered listeners.

If the selection mode is SINGLE_SELECTION, only the lead item is selected.

Parameters:
anchor - the anchor index.
lead - the lead index.

setSelectionMode

public void setSelectionMode(int mode)
Sets the selection mode.

FIXME: The spec is silent about what happens to existing selections, for example when changing from an interval selection to single selection.

Throws:
IllegalArgumentException - if mode is not one of the specified values.

setValueIsAdjusting

public void setValueIsAdjusting(boolean valueIsAdjusting)
Sets the flag that is passed to listeners for each change notification. If a sequence of changes is made to the selection model, this flag should be set to true at the start of the sequence, and false for the last change - this gives listeners the option to ignore interim changes if that is more efficient.
Parameters:
valueIsAdjusting - the flag value.

ListSelectionModel.java -- Copyright (C) 2002, 2006, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.