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

public Class ChoiceBoxListCell<T>

extends ListCell<T>
Class Inheritance
Type Parameters
<T>
The type of the elements contained within the ListView.
Static Imports
javafx.scene.control.cell.CellUtils.createChoiceBox

A class containing a ListCell implementation that draws a ChoiceBox node inside the cell.

By default, the ChoiceBoxListCell is rendered as a Label when not being edited, and as a ChoiceBox when in editing mode. The ChoiceBox will, by default, stretch to fill the entire list cell.

To create a ChoiceBoxListCell, it is necessary to provide zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items sequence, such that upon selection, they replace the existing value in the items list.

Since
JavaFX 2.2

Field Summary

Modifier and TypeField and Description
private ChoiceBox<T>
private ObjectProperty<StringConverter<T>>
private final ObservableList<T>

Constructor Summary

AccessConstructor and Description
public
ChoiceBoxListCell()

Creates a default ChoiceBoxListCell with an empty items list.

public
ChoiceBoxListCell(T...
The items to show in the ChoiceBox popup menu when selected by the user.
items
)

Creates a default ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown.

public
ChoiceBoxListCell(StringConverter<T>
A StringConverter that can convert an item of type T into a user-readable string so that it may then be shown in the ChoiceBox popup menu.
converter
,
T...
The items to show in the ChoiceBox popup menu when selected by the user.
items
)

Creates a ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown, and the StringConverter being used to convert the item in to a user-readable form.

public
ChoiceBoxListCell(ObservableList<T>
The items to show in the ChoiceBox popup menu when selected by the user.
items
)

Creates a default ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown.

public
ChoiceBoxListCell(StringConverter<T>
A StringConverter that can convert an item of type T into a user-readable string so that it may then be shown in the ChoiceBox popup menu.
converter
,
ObservableList<T>
The items to show in the ChoiceBox popup menu when selected by the user.
items
)

Creates a ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown, and the StringConverter being used to convert the item in to a user-readable form.

Method Summary

Modifier and TypeMethod and Description
public void
cancelEdit()

Overrides javafx.scene.control.ListCell.cancelEdit.

Cancels an edit to the value of the cell.

public final ObjectProperty<StringConverter<T>>

Returns:

the StringConverter property
converterProperty
()

The StringConverter property.

public static <
The type of the elements contained within the ListView.
T
>
Callback<ListView<T>, ListCell<T>>

Returns:

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.
forListView
(final T...
Zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items list, such that upon selection, they replace the existing value in the items list.
items
)

Creates a ChoiceBox cell factory for use in ListView controls.

public static <
The type of the elements contained within the ListView.
T
>
Callback<ListView<T>, ListCell<T>>

Returns:

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.
forListView
(final StringConverter<T>
A StringConverter to convert the given item (of type T) to a String for displaying to the user.
converter
,
final T...
Zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items list, such that upon selection, they replace the existing value in the items list.
items
)

Creates a ChoiceBox cell factory for use in ListView controls.

public static <
The type of the elements contained within the ListView.
T
>
Callback<ListView<T>, ListCell<T>>

Returns:

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.
forListView
(final ObservableList<T>
An ObservableList containing zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items sequence, such that upon selection, they replace the existing value in the items list.
items
)

Creates a ChoiceBox cell factory for use in ListView controls.

public static <
The type of the elements contained within the ListView.
T
>
Callback<ListView<T>, ListCell<T>>

Returns:

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.
forListView
(final StringConverter<T>
A StringConverter to convert the given item (of type T) to a String for displaying to the user.
converter
,
final ObservableList<T>
An ObservableList containing zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items sequence, such that upon selection, they replace the existing value in the items list.
items
)

Creates a ChoiceBox cell factory for use in ListView controls.

public final StringConverter<T>

Returns:

the StringConverter used in this cell
getConverter
()

Returns the StringConverter used in this cell.

public ObservableList<T>

Returns:

the items to be displayed in the ChoiceBox when it is showing
getItems
()

Returns the items to be displayed in the ChoiceBox when it is showing.

public final void
setConverter(StringConverter<T>
the StringConverter to be used in this cell
value
)

Sets the StringConverter to be used in this cell.

public void
startEdit()

Overrides javafx.scene.control.ListCell.startEdit.

Starts an edit to the value of the cell.

public void
updateItem(T item, boolean empty)

Overrides javafx.scene.control.Cell.updateItem.

The updateItem method should not be called by developers, but it is the best method for developers to override to allow for them to customise the visuals of the cell.

Inherited from javafx.scene.control.ListCell:
commitEditcreateDefaultSkinexecuteAccessibleActiongetListViewlayoutChildrenlistViewPropertyqueryAccessibleAttributeupdateListView

Field Detail

choiceBoxback to summary
private ChoiceBox<T> choiceBox
converterback to summary
private ObjectProperty<StringConverter<T>> converter
itemsback to summary
private final ObservableList<T> items

Constructor Detail

ChoiceBoxListCellback to summary
public ChoiceBoxListCell()

Creates a default ChoiceBoxListCell with an empty items list.

ChoiceBoxListCellback to summary
public ChoiceBoxListCell(T... items)

Creates a default ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown.

Parameters
items:T[]

The items to show in the ChoiceBox popup menu when selected by the user.

Annotations
@SafeVarargs
ChoiceBoxListCellback to summary
public ChoiceBoxListCell(StringConverter<T> converter, T... items)

Creates a ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown, and the StringConverter being used to convert the item in to a user-readable form.

Parameters
converter:StringConverter<T>

A StringConverter that can convert an item of type T into a user-readable string so that it may then be shown in the ChoiceBox popup menu.

items:T[]

The items to show in the ChoiceBox popup menu when selected by the user.

Annotations
@SafeVarargs
ChoiceBoxListCellback to summary
public ChoiceBoxListCell(ObservableList<T> items)

Creates a default ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown.

Parameters
items:ObservableList<T>

The items to show in the ChoiceBox popup menu when selected by the user.

ChoiceBoxListCellback to summary
public ChoiceBoxListCell(StringConverter<T> converter, ObservableList<T> items)

Creates a ChoiceBoxListCell instance with the given items being used to populate the ChoiceBox when it is shown, and the StringConverter being used to convert the item in to a user-readable form.

Parameters
converter:StringConverter<T>

A StringConverter that can convert an item of type T into a user-readable string so that it may then be shown in the ChoiceBox popup menu.

items:ObservableList<T>

The items to show in the ChoiceBox popup menu when selected by the user.

Method Detail

cancelEditback to summary
public void cancelEdit()

Overrides javafx.scene.control.ListCell.cancelEdit.

Doc from javafx.scene.control.ListCell.cancelEdit.

Cancels an edit to the value of the cell. Call this function to transition from an editing state into a non-editing state, without saving any user input.

Annotations
@Override
converterPropertyback to summary
public final ObjectProperty<StringConverter<T>> converterProperty()

The StringConverter property.

Returns:ObjectProperty<StringConverter<T>>

the StringConverter property

forListViewback to summary
public static <T> Callback<ListView<T>, ListCell<T>> forListView(final T... items)

Creates a ChoiceBox cell factory for use in ListView controls. By default, the ChoiceBoxCell is rendered as a Label when not being edited, and as a ChoiceBox when in editing mode. The ChoiceBox will, by default, stretch to fill the entire list cell.

Parameters
<T>
The type of the elements contained within the ListView.
items:T[]

Zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items list, such that upon selection, they replace the existing value in the items list.

Returns:Callback<ListView<T>, ListCell<T>>

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.

Annotations
@SafeVarargs
forListViewback to summary
public static <T> Callback<ListView<T>, ListCell<T>> forListView(final StringConverter<T> converter, final T... items)

Creates a ChoiceBox cell factory for use in ListView controls. By default, the ChoiceBoxCell is rendered as a Label when not being edited, and as a ChoiceBox when in editing mode. The ChoiceBox will, by default, stretch to fill the entire list cell.

Parameters
<T>
The type of the elements contained within the ListView.
converter:StringConverter<T>

A StringConverter to convert the given item (of type T) to a String for displaying to the user.

items:T[]

Zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items list, such that upon selection, they replace the existing value in the items list.

Returns:Callback<ListView<T>, ListCell<T>>

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.

Annotations
@SafeVarargs
forListViewback to summary
public static <T> Callback<ListView<T>, ListCell<T>> forListView(final ObservableList<T> items)

Creates a ChoiceBox cell factory for use in ListView controls. By default, the ChoiceBoxCell is rendered as a Label when not being edited, and as a ChoiceBox when in editing mode. The ChoiceBox will, by default, stretch to fill the entire list cell.

Parameters
<T>
The type of the elements contained within the ListView.
items:ObservableList<T>

An ObservableList containing zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items sequence, such that upon selection, they replace the existing value in the items list.

Returns:Callback<ListView<T>, ListCell<T>>

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.

forListViewback to summary
public static <T> Callback<ListView<T>, ListCell<T>> forListView(final StringConverter<T> converter, final ObservableList<T> items)

Creates a ChoiceBox cell factory for use in ListView controls. By default, the ChoiceBoxCell is rendered as a Label when not being edited, and as a ChoiceBox when in editing mode. The ChoiceBox will, by default, stretch to fill the entire list cell.

Parameters
<T>
The type of the elements contained within the ListView.
converter:StringConverter<T>

A StringConverter to convert the given item (of type T) to a String for displaying to the user.

items:ObservableList<T>

An ObservableList containing zero or more items that will be shown to the user when the ChoiceBox menu is showing. These items must be of the same type as the ListView items sequence, such that upon selection, they replace the existing value in the items list.

Returns:Callback<ListView<T>, ListCell<T>>

A Callback that will return a ListCell that is able to work on the type of element contained within the ListView.

getConverterback to summary
public final StringConverter<T> getConverter()

Returns the StringConverter used in this cell.

Returns:StringConverter<T>

the StringConverter used in this cell

getItemsback to summary
public ObservableList<T> getItems()

Returns the items to be displayed in the ChoiceBox when it is showing.

Returns:ObservableList<T>

the items to be displayed in the ChoiceBox when it is showing

setConverterback to summary
public final void setConverter(StringConverter<T> value)

Sets the StringConverter to be used in this cell.

Parameters
value:StringConverter<T>

the StringConverter to be used in this cell

startEditback to summary
public void startEdit()

Overrides javafx.scene.control.ListCell.startEdit.

Doc from javafx.scene.control.ListCell.startEdit.

Starts an edit to the value of the cell. Call this function to transition from a non-editing state into an editing state, if the cell is editable. If this cell is already in an editing state, it will stay in it.

Annotations
@Override
updateItemback to summary
public void updateItem(T item, boolean empty)

Overrides javafx.scene.control.Cell.updateItem.

Doc from javafx.scene.control.Cell.updateItem.

The updateItem method should not be called by developers, but it is the best method for developers to override to allow for them to customise the visuals of the cell. To clarify, developers should never call this method in their code (they should leave it up to the UI control, such as the javafx.scene.control.ListView control) to call this method. However, the purpose of having the updateItem method is so that developers, when specifying custom cell factories (again, like the ListView cell factory), the updateItem method can be overridden to allow for complete customisation of the cell.

It is very important that subclasses of Cell override the updateItem method properly, as failure to do so will lead to issues such as blank cells or cells with unexpected content appearing within them. Here is an example of how to properly override the updateItem method:

protected void updateItem(T item, boolean empty) {
    super.updateItem(item, empty);

    if (empty || item == null) {
        setText(null);
        setGraphic(null);
    } else {
        setText(item.toString());
    }
}

Note in this code sample two important points:

  1. We call the super.updateItem(T, boolean) method. If this is not done, the item and empty properties are not correctly set, and you are likely to end up with graphical issues.
  2. We test for the empty condition, and if true, we set the text and graphic properties to null. If we do not do this, it is almost guaranteed that end users will see graphical artifacts in cells unexpectedly.
Annotations
@Override