APIdia Launch Partner
Top Description Fields Constructors Methods
javafx.scene.control

public abstract Class SelectionModel<T>

extends Object
Class Inheritance
Known Direct Subclasses
javafx.scene.control.SingleSelectionModel, javafx.scene.control.MultipleSelectionModel
Type Parameters
<T>
The type of the item contained in the control that can be selected.
Imports
javafx.beans.property.ReadOnlyIntegerProperty, .ReadOnlyIntegerWrapper, .ReadOnlyObjectProperty, .ReadOnlyObjectWrapper, javafx.collections.ListChangeListener

SelectionModel is an abstract class used by UI controls to provide a consistent API for maintaining selection.
Since
JavaFX 2.0

Field Summary

Modifier and TypeField and Description
private ReadOnlyIntegerWrapper
private ReadOnlyObjectWrapper<T>

Constructor Summary

AccessConstructor and Description
public
SelectionModel()

Creates a default SelectionModel instance.

Method Summary

Modifier and TypeMethod and Description
public abstract void
clearAndSelect(int
The index that should be the only selected index in this selection model.
index
)

A method that clears any selection prior to setting the selection to the given index.

public abstract void
clearSelection(int
The selected item to deselect.
index
)

This method will clear the selection of the item in the given index.

public abstract void
clearSelection()

Clears the selection model of all selected indices.

public final int

Returns:

the selected index
getSelectedIndex
()

Returns the integer value indicating the currently selected index in this model.

public final T

Returns:

the selected item
getSelectedItem
()

Returns the currently selected object (which resides in the selected index position).

public abstract boolean

Returns:

Will return true if there are no selected items, and false if there are.
isEmpty
()

This method is available to test whether there are any selected indices/items.

public abstract boolean

Returns:

True if the given index is selected, false otherwise.
isSelected
(int
The index to check as to whether it is currently selected or not.
index
)

This method tests whether the given index is currently selected in this SelectionModel.

public abstract void
select(int
The position of the item to select in the selection model.
index
)

This will select the given index in the selection model, assuming the index is within the valid range (i.e. greater than or equal to zero, and less than the total number of items in the underlying data model).

public abstract void
select(T
The object to attempt to select in the underlying data model.
obj
)

This method will attempt to select the index that contains the given object.

public final ReadOnlyIntegerProperty

Returns:

the selected index property
selectedIndexProperty
()

Refers to the selected index property, which is used to indicate the currently selected index value in the selection model.

public final ReadOnlyObjectProperty<T>

Returns:

the selected item property
selectedItemProperty
()

Refers to the selected item property, which is used to indicate the currently selected item in the selection model.

public abstract void
selectFirst()

This method will attempt to select the first index in the control.

public abstract void
selectLast()

This method will attempt to select the last index in the control.

public abstract void
selectNext()

This method will attempt to select the index directly after the current focused index.

public abstract void
selectPrevious()

This method will attempt to select the index directly before the current focused index.

protected final void
setSelectedIndex(int value)

protected final void
setSelectedItem(T value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

selectedIndexback to summary
private ReadOnlyIntegerWrapper selectedIndex
selectedItemback to summary
private ReadOnlyObjectWrapper<T> selectedItem

Constructor Detail

SelectionModelback to summary
public SelectionModel()

Creates a default SelectionModel instance.

Method Detail

clearAndSelectback to summary
public abstract void clearAndSelect(int index)

A method that clears any selection prior to setting the selection to the given index. The purpose of this method is to avoid having to call clearSelection() first, meaning that observers that are listening to the selected index property will not see the selected index being temporarily set to -1.

Parameters
index:int

The index that should be the only selected index in this selection model.

clearSelectionback to summary
public abstract void clearSelection(int index)

This method will clear the selection of the item in the given index. If the given index is not selected, nothing will happen.

Parameters
index:int

The selected item to deselect.

clearSelectionback to summary
public abstract void clearSelection()

Clears the selection model of all selected indices.

getSelectedIndexback to summary
public final int getSelectedIndex()

Returns the integer value indicating the currently selected index in this model. If there are multiple items selected, this will return the most recent selection made.

Note that the returned value is a snapshot in time - if you wish to observe the selection model for changes to the selected index, you can add a ChangeListener as such:


SelectionModel sm = ...;
InvalidationListener listener = ...;
sm.selectedIndexProperty().addListener(listener);
Returns:int

the selected index

getSelectedItemback to summary
public final T getSelectedItem()

Returns the currently selected object (which resides in the selected index position). If there are multiple items selected, this will return the object contained at the index returned by getSelectedIndex() (which is always the index to the most recently selected item).

Note that the returned value is a snapshot in time - if you wish to observe the selection model for changes to the selected item, you can add a ChangeListener as such:


SelectionModel sm = ...;
InvalidationListener listener = ...;
sm.selectedItemProperty().addListener(listener);
Returns:T

the selected item

isEmptyback to summary
public abstract boolean isEmpty()

This method is available to test whether there are any selected indices/items. It will return true if there are no selected items, and false if there are.

Returns:boolean

Will return true if there are no selected items, and false if there are.

isSelectedback to summary
public abstract boolean isSelected(int index)

This method tests whether the given index is currently selected in this SelectionModel.

Parameters
index:int

The index to check as to whether it is currently selected or not.

Returns:boolean

True if the given index is selected, false otherwise.

selectback to summary
public abstract void select(int index)

This will select the given index in the selection model, assuming the index is within the valid range (i.e. greater than or equal to zero, and less than the total number of items in the underlying data model).

If there is already one or more indices selected in this model, calling this method will not clear these selections - to do so it is necessary to first call clearSelection().

If the index is already selected, it will not be selected again, or unselected. However, if multiple selection is implemented, then calling select on an already selected index will have the effect of making the index the new selected index (as returned by getSelectedIndex().

Parameters
index:int

The position of the item to select in the selection model.

selectback to summary
public abstract void select(T obj)

This method will attempt to select the index that contains the given object. It will iterate through the underlying data model until it finds an item whose value is equal to the given object. At this point it will stop iterating - this means that this method will not select multiple indices.

Parameters
obj:T

The object to attempt to select in the underlying data model.

selectedIndexPropertyback to summary
public final ReadOnlyIntegerProperty selectedIndexProperty()

Refers to the selected index property, which is used to indicate the currently selected index value in the selection model. The selected index is either -1, to represent that there is no selection, or an integer value that is within the range of the underlying data model size.

The selected index property is most commonly used when the selection model only allows single selection, but is equally applicable when in multiple selection mode. When in this mode, the selected index will always represent the last selection made.

Note that in the case of multiple selection, it is possible to add a ListChangeListener to the collection returned by MultipleSelectionModel#getSelectedIndices() to be informed whenever the selection changes, and this will also work in the case of single selection.

Returns:ReadOnlyIntegerProperty

the selected index property

selectedItemPropertyback to summary
public final ReadOnlyObjectProperty<T> selectedItemProperty()

Refers to the selected item property, which is used to indicate the currently selected item in the selection model. The selected item is either null, to represent that there is no selection, or an Object that is retrieved from the underlying data model of the control the selection model is associated with.

The selected item property is most commonly used when the selection model is set to be single selection, but is equally applicable when in multiple selection mode. When in this mode, the selected item will always represent the last selection made.

Returns:ReadOnlyObjectProperty<T>

the selected item property

selectFirstback to summary
public abstract void selectFirst()

This method will attempt to select the first index in the control. If clearSelection is not called first, this method will have the result of selecting the first index, whilst retaining the selection of any other currently selected indices.

If the first index is already selected, calling this method will have no result, and no selection event will take place.

selectLastback to summary
public abstract void selectLast()

This method will attempt to select the last index in the control. If clearSelection is not called first, this method will have the result of selecting the last index, whilst retaining the selection of any other currently selected indices.

If the last index is already selected, calling this method will have no result, and no selection event will take place.

selectNextback to summary
public abstract void selectNext()

This method will attempt to select the index directly after the current focused index. If clearSelection is not called first, this method will have the result of selecting the next index, whilst retaining the selection of any other currently selected indices.

Calling this method will only succeed if:

  • There is currently a lead/focused index.
  • The lead/focus index is not the last index in the control.
  • The next index is not already selected.

If any of these conditions is false, no selection event will take place.

selectPreviousback to summary
public abstract void selectPrevious()

This method will attempt to select the index directly before the current focused index. If clearSelection is not called first, this method will have the result of selecting the previous index, whilst retaining the selection of any other currently selected indices.

Calling this method will only succeed if:

  • There is currently a lead/focused index.
  • The lead/focus index is not the first index in the control.
  • The previous index is not already selected.

If any of these conditions is false, no selection event will take place.

setSelectedIndexback to summary
protected final void setSelectedIndex(int value)
setSelectedItemback to summary
protected final void setSelectedItem(T value)