javax.swing

Class DefaultListSelectionModel

public class DefaultListSelectionModel extends Object implements Cloneable, ListSelectionModel, Serializable

The default implementation of {@link ListSelectionModel}, which is used by {@link javax.swing.JList} and similar classes to manage the selection status of a number of data elements.

The class is organized abstractly as a set of intervals of integers. Each interval indicates an inclusive range of indices in a list -- held by some other object and unknown to this class -- which is considered "selected". There are various accessors for querying and modifying the set of intervals, with simplified forms accepting a single index, representing an interval with only one element.

Field Summary
protected booleanleadAnchorNotificationEnabled
controls the range of indices provided in any {@link ListSelectionEvent} fired by the selectionModel.
protected EventListenerListlistenerList
The list of ListSelectionListeners subscribed to this selection model.
Method Summary
voidaddListSelectionListener(ListSelectionListener listener)
Adds a listener.
voidaddSelectionInterval(int index0, int index1)
If the {@link #selectionMode} property is equal to SINGLE_SELECTION equivalent to calling setSelectionInterval(index1, index2); If the {@link #selectionMode} property is equal to SINGLE_INTERVAL_SELECTION and the interval being added is not adjacent to an already selected interval, equivalent to setSelectionInterval(index1, index2).
voidclearSelection()
Removes all intervals in the selection set.
Objectclone()
Returns a clone of this object.
protected voidfireValueChanged(boolean isAdjusting)
Fires a {@link ListSelectionEvent} to all the listeners of type {@link ListSelectionListener} registered with this selection model to indicate that a series of adjustment has just ended.
protected voidfireValueChanged(int firstIndex, int lastIndex)
Fires a {@link ListSelectionEvent} to all the listeners of type {@link ListSelectionListener} registered with this selection model.
protected voidfireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
Fires a {@link ListSelectionEvent} to all the listeners of type {@link ListSelectionListener} registered with this selection model.
intgetAnchorSelectionIndex()
Gets the value of the {@link #anchorSelectionIndex} property.
intgetLeadSelectionIndex()
Gets the value of the {@link #leadSelectionIndex} property.
<T extends EventListener> T[]getListeners(Class<T> listenerType)
Returns an array of all registerers listeners.
ListSelectionListener[]getListSelectionListeners()
Returns an array of all registerd list selection listeners.
intgetMaxSelectionIndex()
Gets the largest index which is currently a member of a selection interval.
intgetMinSelectionIndex()
Gets the smallest index which is currently a member of a selection interval.
intgetSelectionMode()
Returns the selection mode, which is one of {@link #SINGLE_SELECTION}, {@link #SINGLE_INTERVAL_SELECTION} and {@link #MULTIPLE_INTERVAL_SELECTION}.
booleangetValueIsAdjusting()
Gets the value of the {@link #valueIsAdjusting} property.
voidinsertIndexInterval(int index, int length, boolean before)
Inserts a number of indices either before or after a particular position in the set of indices.
booleanisLeadAnchorNotificationEnabled()
Gets the value of the {@link #leadAnchorNotificationEnabled} property.
booleanisSelectedIndex(int a)
Determines whether a particular index is a member of a selection interval.
booleanisSelectionEmpty()
Determines whether the selection is empty.
voidmoveLeadSelectionIndex(int leadIndex)
Moves the lead selection index to leadIndex without changing the selection values.
voidremoveIndexInterval(int index0, int index1)
Removes a range from the set of indices.
voidremoveListSelectionListener(ListSelectionListener listener)
Removes a registered listener.
voidremoveSelectionInterval(int index0, int index1)
Deselects all indices in the inclusive range [index0,index1].
voidsetAnchorSelectionIndex(int index)
Sets the value of the {@link #anchorSelectionIndex} property.
voidsetLeadAnchorNotificationEnabled(boolean l)
Sets the value of the {@link #leadAnchorNotificationEnabled} property.
voidsetLeadSelectionIndex(int leadIndex)

Sets the value of the {@link #anchorSelectionIndex} property.

voidsetSelectionInterval(int anchor, int lead)
Clears the current selection and marks a given interval as "selected".
voidsetSelectionMode(int mode)
Sets the value of the {@link #selectionMode} property.
voidsetValueIsAdjusting(boolean v)
Sets the value of the {@link #valueIsAdjusting} property.

Field Detail

leadAnchorNotificationEnabled

protected boolean leadAnchorNotificationEnabled
controls the range of indices provided in any {@link ListSelectionEvent} fired by the selectionModel. Let [A,L] be the range of indices between {@link #anchorSelectionIndex} and {@link #leadSelectionIndex} inclusive, and let [i0,i1] be the range of indices changed in a given call which generates a {@link ListSelectionEvent}. Then when this property is true, the {@link ListSelectionEvent} contains the range [A,L] union [i0,i1]; when false it will contain only [i0,i1]. The default is true.

See Also: DefaultListSelectionModel DefaultListSelectionModel

listenerList

protected EventListenerList listenerList
The list of ListSelectionListeners subscribed to this selection model.

Method Detail

addListSelectionListener

public void addListSelectionListener(ListSelectionListener listener)
Adds a listener.

Parameters: listener The listener to add

See Also: DefaultListSelectionModel DefaultListSelectionModel

addSelectionInterval

public void addSelectionInterval(int index0, int index1)
If the {@link #selectionMode} property is equal to SINGLE_SELECTION equivalent to calling setSelectionInterval(index1, index2); If the {@link #selectionMode} property is equal to SINGLE_INTERVAL_SELECTION and the interval being added is not adjacent to an already selected interval, equivalent to setSelectionInterval(index1, index2). Otherwise adds the range [index0, index1] to the selection interval set.

Parameters: index0 The beginning of the range of indices to select index1 The end of the range of indices to select

See Also: DefaultListSelectionModel DefaultListSelectionModel

clearSelection

public void clearSelection()
Removes all intervals in the selection set.

clone

public Object clone()
Returns a clone of this object. listenerList don't gets duplicated.

Returns: the cloned object

Throws: CloneNotSupportedException if an error occurs

fireValueChanged

protected void fireValueChanged(boolean isAdjusting)
Fires a {@link ListSelectionEvent} to all the listeners of type {@link ListSelectionListener} registered with this selection model to indicate that a series of adjustment has just ended. The values of {@link #getMinSelectionIndex} and {@link #getMaxSelectionIndex} are used in the {@link ListSelectionEvent} that gets fired.

Parameters: isAdjusting true if this is the final change in a series of adjustments, false/code> otherwise

fireValueChanged

protected void fireValueChanged(int firstIndex, int lastIndex)
Fires a {@link ListSelectionEvent} to all the listeners of type {@link ListSelectionListener} registered with this selection model.

Parameters: firstIndex The low index of the changed range lastIndex The high index of the changed range

fireValueChanged

protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
Fires a {@link ListSelectionEvent} to all the listeners of type {@link ListSelectionListener} registered with this selection model.

Parameters: firstIndex The low index of the changed range lastIndex The high index of the changed range isAdjusting Whether this change is part of a seqence of adjustments made to the selection, such as during interactive scrolling

getAnchorSelectionIndex

public int getAnchorSelectionIndex()
Gets the value of the {@link #anchorSelectionIndex} property.

Returns: The current property value

See Also: DefaultListSelectionModel

getLeadSelectionIndex

public int getLeadSelectionIndex()
Gets the value of the {@link #leadSelectionIndex} property.

Returns: The current property value

See Also: DefaultListSelectionModel

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all registerers listeners.

Parameters: listenerType The type of listener to retrieve

Returns: The array

Since: 1.3

See Also: DefaultListSelectionModel

getListSelectionListeners

public ListSelectionListener[] getListSelectionListeners()
Returns an array of all registerd list selection listeners.

Returns: the array

Since: 1.4

See Also: DefaultListSelectionModel DefaultListSelectionModel DefaultListSelectionModel

getMaxSelectionIndex

public int getMaxSelectionIndex()
Gets the largest index which is currently a member of a selection interval.

Returns: The greatest integer i such that i >= 0 and i is a member of a selected interval, or -1 if there are no selected intervals

See Also: DefaultListSelectionModel

getMinSelectionIndex

public int getMinSelectionIndex()
Gets the smallest index which is currently a member of a selection interval.

Returns: The least integer i such that i >= 0 and i is a member of a selected interval, or -1 if there are no selected intervals

See Also: DefaultListSelectionModel

getSelectionMode

public int getSelectionMode()
Returns the selection mode, which is one of {@link #SINGLE_SELECTION}, {@link #SINGLE_INTERVAL_SELECTION} and {@link #MULTIPLE_INTERVAL_SELECTION}. The default value is {@link #MULTIPLE_INTERVAL_SELECTION}.

Returns: The selection mode.

See Also: DefaultListSelectionModel

getValueIsAdjusting

public boolean getValueIsAdjusting()
Gets the value of the {@link #valueIsAdjusting} property.

Returns: The current property value

See Also: DefaultListSelectionModel

insertIndexInterval

public void insertIndexInterval(int index, int length, boolean before)
Inserts a number of indices either before or after a particular position in the set of indices. Renumbers all indices after the inserted range. The new indices in the inserted range are not selected. This method is typically called to synchronize the selection model with an inserted range of elements in a {@link ListModel}.

Parameters: index The position to insert indices at length The number of indices to insert before Indicates whether to insert the indices before the index or after it

isLeadAnchorNotificationEnabled

public boolean isLeadAnchorNotificationEnabled()
Gets the value of the {@link #leadAnchorNotificationEnabled} property.

Returns: The current property value

See Also: DefaultListSelectionModel

isSelectedIndex

public boolean isSelectedIndex(int a)
Determines whether a particular index is a member of a selection interval.

Parameters: a The index to search for

Returns: true if the index is a member of a selection interval, otherwise false

isSelectionEmpty

public boolean isSelectionEmpty()
Determines whether the selection is empty.

Returns: true if the selection is empty, otherwise false

moveLeadSelectionIndex

public void moveLeadSelectionIndex(int leadIndex)
Moves the lead selection index to leadIndex without changing the selection values. If leadAnchorNotificationEnabled is true, send a notification covering the old and new lead cells.

Parameters: leadIndex the new lead selection index

Since: 1.5

removeIndexInterval

public void removeIndexInterval(int index0, int index1)
Removes a range from the set of indices. Renumbers all indices after the removed range. This method is typically called to synchronize the selection model with a deleted range of elements in a {@link ListModel}.

Parameters: index0 The first index to remove (inclusive) index1 The last index to remove (inclusive)

removeListSelectionListener

public void removeListSelectionListener(ListSelectionListener listener)
Removes a registered listener.

Parameters: listener The listener to remove

See Also: DefaultListSelectionModel DefaultListSelectionModel

removeSelectionInterval

public void removeSelectionInterval(int index0, int index1)
Deselects all indices in the inclusive range [index0,index1].

Parameters: index0 The beginning of the range of indices to deselect index1 The end of the range of indices to deselect

See Also: DefaultListSelectionModel DefaultListSelectionModel

setAnchorSelectionIndex

public void setAnchorSelectionIndex(int index)
Sets the value of the {@link #anchorSelectionIndex} property.

Parameters: index The new property value

See Also: DefaultListSelectionModel

setLeadAnchorNotificationEnabled

public void setLeadAnchorNotificationEnabled(boolean l)
Sets the value of the {@link #leadAnchorNotificationEnabled} property.

Parameters: l The new property value

See Also: DefaultListSelectionModel

setLeadSelectionIndex

public void setLeadSelectionIndex(int leadIndex)

Sets the value of the {@link #anchorSelectionIndex} property. As a side effect, alters the selection status of two ranges of indices. Let OL be the old lead selection index, NL be the new lead selection index, and A be the anchor selection index. Then if A is a valid selection index, one of two things happens depending on the seleciton status of A:

This method generates at most a single {@link ListSelectionEvent} despite changing multiple ranges. The range of values provided to the {@link ListSelectionEvent} includes only the minimum range of values which changed selection status between the beginning and end of the method.

Parameters: leadIndex The new property value

See Also: DefaultListSelectionModel

setSelectionInterval

public void setSelectionInterval(int anchor, int lead)
Clears the current selection and marks a given interval as "selected". If the current selection mode is SINGLE_SELECTION only the index index2 is selected.

Parameters: anchor the anchor selection index. lead the lead selection index.

setSelectionMode

public void setSelectionMode(int mode)
Sets the value of the {@link #selectionMode} property.

Parameters: mode The new value of the property

setValueIsAdjusting

public void setValueIsAdjusting(boolean v)
Sets the value of the {@link #valueIsAdjusting} property.

Parameters: v The new property value

See Also: DefaultListSelectionModel