Top Description Inners Fields Constructors Methods
javafx.scene.control

public Class ComboBox<T>

extends ComboBoxBase<T>
Class Inheritance
Type Parameters
<T>
The type of the value that has been selected or otherwise entered in to this ComboBox
Imports
com.sun.javafx.scene.control.FakeFocusTextField, javafx.beans.InvalidationListener, .Observable, .WeakInvalidationListener, javafx.collections.WeakListChangeListener, .FXCollections, .ListChangeListener, .ObservableList, javafx.scene.control.skin.ComboBoxListViewSkin, javafx.beans.property.*, javafx.beans.value.ChangeListener, .ObservableValue, javafx.scene.AccessibleAttribute, .AccessibleRole, .Node, javafx.util.Callback, .StringConverter, java.lang.ref.WeakReference

An implementation of the ComboBoxBase abstract class for the most common form of ComboBox, where a popup list is shown to users providing them with a choice that they may select from. For more information around the general concepts and API of ComboBox, refer to the ComboBoxBase class documentation.

On top of ComboBoxBase, the ComboBox class introduces additional API. Most importantly, it adds an items property that works in much the same way as the ListView items property. In other words, it is the content of the items list that is displayed to users when they click on the ComboBox button.

The ComboBox exposes the valueProperty() from javafx.scene.control.ComboBoxBase, but there are some important points of the value property that need to be understood in relation to ComboBox. These include:

  1. The value property is not constrained to items contained within the items list - it can be anything as long as it is a valid value of type T.
  2. If the value property is set to a non-null object, and subsequently the items list is cleared, the value property is not nulled out.
  3. Clearing the selection in the selection model does not null the value property - it remains the same as before.
  4. It is valid for the selection model to have a selection set to a given index even if there is no items in the list (or less items in the list than the given index). Once the items list is further populated, such that the list contains enough items to have an item in the given index, both the selection model SelectionModel#selectedItemProperty() and value property will be updated to have this value. This is inconsistent with other controls that use a selection model, but done intentionally for ComboBox.

By default, when the popup list is showing, the maximum number of rows visible is 10, but this can be changed by modifying the visibleRowCount property. If the number of items in the ComboBox is less than the value of visibleRowCount, then the items size will be used instead so that the popup list is not exceedingly long.

As with ListView, it is possible to modify the selection model that is used, although this is likely to be rarely changed. This is because the ComboBox enforces the need for a javafx.scene.control.SingleSelectionModel instance, and it is not likely that there is much need for alternate implementations. Nonetheless, the option is there should use cases be found for switching the selection model.

As the ComboBox internally renders content with a ListView, API exists in the ComboBox class to allow for a custom cell factory to be set. For more information on cell factories, refer to the Cell and ListCell classes. It is important to note that if a cell factory is set on a ComboBox, cells will only be used in the ListView that shows when the ComboBox is clicked. If you also want to customize the rendering of the 'button' area of the ComboBox, you can set a custom ListCell instance in the button cell property. One way of doing this is with the following code (note the use of setButtonCell:

 Callback<ListView<String>, ListCell<String>> cellFactory = ...;
 ComboBox comboBox = new ComboBox();
 comboBox.setItems(items);
 comboBox.setButtonCell(cellFactory.call(null));
 comboBox.setCellFactory(cellFactory);

Because a ComboBox can be editable, and the default means of allowing user input is via a TextField, a string converter property is provided to allow for developers to specify how to translate a users string into an object of type T, such that the value property may contain it. By default the converter simply returns the String input as the user typed it, which therefore assumes that the type of the editable ComboBox is String. If a different type is specified and the ComboBox is to be editable, it is necessary to specify a custom StringConverter.

Warning: Nodes should not be inserted directly into the ComboBox items list

ComboBox allows for the items list to contain elements of any type, including Node instances. Putting nodes into the items list is strongly discouraged, as it can lead to unexpected results. This is because the default cell factory simply inserts Node items directly into the cell, including in the ComboBox 'button' area too. Because the scenegraph only allows for Nodes to be in one place at a time, this means that when an item is selected it becomes removed from the ComboBox list, and becomes visible in the button area. When selection changes the previously selected item returns to the list and the new selection is removed.

Important points to note:

The following minimal example shows how to create a custom cell factory for ComboBox containing Nodes:

 ComboBox<Color> cmb = new ComboBox<>();
 cmb.getItems().addAll(
     Color.RED,
     Color.GREEN,
     Color.BLUE);

 cmb.setCellFactory(p -> {
     return new ListCell<>() {
         private final Rectangle rectangle;
         {
             setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
             rectangle = new Rectangle(10, 10);
         }

         @Override protected void updateItem(Color item, boolean empty) {
             super.updateItem(item, empty);

             if (item == null || empty) {
                 setGraphic(null);
             } else {
                 rectangle.setFill(item);
                 setGraphic(rectangle);
             }
         }
     };
 });

This example has an anonymous custom ListCell class in the custom cell factory. Note that the Rectangle (Node) object needs to be created in the instance initialization block or the constructor of the custom ListCell class and updated/used in its updateItem method. Image of the ComboBox control

Admittedly the above approach is far more verbose, but it offers the required functionality without encountering the scenegraph constraints.

Since
JavaFX 2.1
See Also
ComboBoxBase, Cell, ListCell, StringConverter

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final ObjectProperty<ListCell<T>>
private final ObjectProperty<Callback<ListView<T>, ListCell<T>>>
cellFactory

Providing a custom cell factory allows for complete customization of the rendering of items in the ComboBox.

private final ObjectProperty<StringConverter<T>>
private static final String
private ReadOnlyObjectWrapper<TextField>
editor

The editor for the ComboBox.

private final ObjectProperty<ObservableList<T>>
items

The list of items to show within the ComboBox popup.

private ObjectProperty<Node>
private int
private ChangeListener<T>
private ObjectProperty<SingleSelectionModel<T>>
selectionModel

The selection model for the ComboBox.

private TextField
private IntegerProperty
visibleRowCount

The maximum number of rows to be visible in the ComboBox popup when it is showing.

private boolean
Inherited from javafx.scene.control.ComboBoxBase:
ON_HIDDENON_HIDINGON_SHOWINGON_SHOWN

Constructor Summary

AccessConstructor and Description
public
ComboBox()

Creates a default ComboBox instance with an empty items list and default selection model.

public
ComboBox(ObservableList<T>
the list of items
items
)

Creates a default ComboBox instance with the provided items list and a default selection model.

Method Summary

Modifier and TypeMethod and Description
public final ObjectProperty<ListCell<T>>

Returns:

the button cell property
buttonCellProperty
()

The button cell is used to render what is shown in the ComboBox 'button' area.

public final void
cancelEdit()

If the ComboBox is editable, calling this method will attempt to replace the editor text with the last committed value.

public final ObjectProperty<Callback<ListView<T>, ListCell<T>>>
public final void
commitValue()

If the ComboBox is editable, calling this method will attempt to commit the current text and convert it to a value.

public final ObjectProperty<StringConverter<T>>

Returns:

the converter property
converterProperty
()

Converts the user-typed input (when the ComboBox is editable) to an object of type T, such that the input may be retrieved via the value property.

protected Skin<?>
createDefaultSkin()

Overrides javafx.scene.control.Control.createDefaultSkin.

Create a new instance of the default skin for this control.

private static <T> StringConverter<T>
public final ReadOnlyObjectProperty<TextField>
public final ListCell<T>
public final Callback<ListView<T>, ListCell<T>>
public final StringConverter<T>
public final TextField
public final ObservableList<T>
public final Node
public final SingleSelectionModel<T>
public final int
public final ObjectProperty<ObservableList<T>>
public final ObjectProperty<Node>

Returns:

the placeholder property
placeholderProperty
()

This Node is shown to the user when the ComboBox has no content to show.

public Object
queryAccessibleAttribute(AccessibleAttribute
the requested attribute
attribute
,
Object...
optional list of parameters
parameters
)

Overrides javafx.scene.control.ComboBoxBase.queryAccessibleAttribute.

This method is called by the assistive technology to request the value for an attribute.

public final ObjectProperty<SingleSelectionModel<T>>
public final void
public final void
public final void
public final void
public final void
public final void
public final void
setVisibleRowCount(int value)

private void
updateValue(T newValue)

public final IntegerProperty
Inherited from javafx.scene.control.ComboBoxBase:
armarmedPropertydisarmeditablePropertyexecuteAccessibleActiongetOnActiongetOnHiddengetOnHidinggetOnShowinggetOnShowngetPromptTextgetValuehideisArmedisEditableisShowingonActionPropertyonHiddenPropertyonHidingPropertyonShowingPropertyonShownPropertypromptTextPropertysetEditablesetOnActionsetOnHiddensetOnHidingsetOnShowingsetOnShownsetPromptTextsetValueshowshowingPropertyvalueProperty

Field Detail

buttonCellback to summary
private final ObjectProperty<ListCell<T>> buttonCell
cellFactoryback to summary
private final ObjectProperty<Callback<ListView<T>, ListCell<T>>> cellFactory

Providing a custom cell factory allows for complete customization of the rendering of items in the ComboBox. Refer to the Cell javadoc for more information on cell factories.

converterback to summary
private final ObjectProperty<StringConverter<T>> converter
DEFAULT_STYLE_CLASSback to summary
private static final String DEFAULT_STYLE_CLASS

Hides javafx.scene.control.ComboBoxBase.DEFAULT_STYLE_CLASS.

editorback to summary
private ReadOnlyObjectWrapper<TextField> editor

The editor for the ComboBox. The editor is null if the ComboBox is not editable.

Since
JavaFX 2.2
itemsback to summary
private final ObjectProperty<ObservableList<T>> items

The list of items to show within the ComboBox popup.

placeholderback to summary
private ObjectProperty<Node> placeholder
previousItemCountback to summary
private int previousItemCount
selectedItemListenerback to summary
private ChangeListener<T> selectedItemListener
selectionModelback to summary
private ObjectProperty<SingleSelectionModel<T>> selectionModel

The selection model for the ComboBox. A ComboBox only supports single selection.

textFieldback to summary
private TextField textField
visibleRowCountback to summary
private IntegerProperty visibleRowCount

The maximum number of rows to be visible in the ComboBox popup when it is showing. By default this value is 10, but this can be changed to increase or decrease the height of the popup.

wasSetAllCalledback to summary
private boolean wasSetAllCalled

Constructor Detail

ComboBoxback to summary
public ComboBox()

Creates a default ComboBox instance with an empty items list and default selection model.

ComboBoxback to summary
public ComboBox(ObservableList<T> items)

Creates a default ComboBox instance with the provided items list and a default selection model.

Parameters
items:ObservableList<T>

the list of items

Method Detail

buttonCellPropertyback to summary
public final ObjectProperty<ListCell<T>> buttonCellProperty()

The button cell is used to render what is shown in the ComboBox 'button' area. If a cell is set here, it does not change the rendering of the ComboBox popup list - that rendering is controlled via the cell factory API.

Returns:ObjectProperty<ListCell<T>>

the button cell property

Since
JavaFX 2.2
cancelEditback to summary
public final void cancelEdit()

If the ComboBox is editable, calling this method will attempt to replace the editor text with the last committed value.

Since
9
cellFactoryPropertyback to summary
public final ObjectProperty<Callback<ListView<T>, ListCell<T>>> cellFactoryProperty()
commitValueback to summary
public final void commitValue()

If the ComboBox is editable, calling this method will attempt to commit the current text and convert it to a value.

Since
9
converterPropertyback to summary
public final ObjectProperty<StringConverter<T>> converterProperty()

Converts the user-typed input (when the ComboBox is editable) to an object of type T, such that the input may be retrieved via the value property.

Returns:ObjectProperty<StringConverter<T>>

the converter property

createDefaultSkinback to summary
protected Skin<?> createDefaultSkin()

Overrides javafx.scene.control.Control.createDefaultSkin.

Doc from javafx.scene.control.Control.createDefaultSkin.

Create a new instance of the default skin for this control. This is called to create a skin for the control if no skin is provided via CSS -fx-skin or set explicitly in a sub-class with setSkin(...).

Returns:Skin<?>

new instance of default skin for this control. If null then the control will have no skin unless one is provided by css.

Annotations
@Override
defaultStringConverterback to summary
private static <T> StringConverter<T> defaultStringConverter()
editorPropertyback to summary
public final ReadOnlyObjectProperty<TextField> editorProperty()
getButtonCellback to summary
public final ListCell<T> getButtonCell()
getCellFactoryback to summary
public final Callback<ListView<T>, ListCell<T>> getCellFactory()
getConverterback to summary
public final StringConverter<T> getConverter()
getEditorback to summary
public final TextField getEditor()
getItemsback to summary
public final ObservableList<T> getItems()
getPlaceholderback to summary
public final Node getPlaceholder()
getSelectionModelback to summary
public final SingleSelectionModel<T> getSelectionModel()
getVisibleRowCountback to summary
public final int getVisibleRowCount()
itemsPropertyback to summary
public final ObjectProperty<ObservableList<T>> itemsProperty()
placeholderPropertyback to summary
public final ObjectProperty<Node> placeholderProperty()

This Node is shown to the user when the ComboBox has no content to show. The placeholder node is shown in the ComboBox popup area when the items list is null or empty.

Returns:ObjectProperty<Node>

the placeholder property

Since
JavaFX 8.0
queryAccessibleAttributeback to summary
public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters)

Overrides javafx.scene.control.ComboBoxBase.queryAccessibleAttribute.

Doc from javafx.scene.control.ComboBoxBase.queryAccessibleAttribute.

This method is called by the assistive technology to request the value for an attribute.

This method is commonly overridden by subclasses to implement attributes that are required for a specific role.
If a particular attribute is not handled, the superclass implementation must be called.

Parameters
attribute:AccessibleAttribute

Doc from javafx.scene.Node.queryAccessibleAttribute.

the requested attribute

parameters:Object[]

Doc from javafx.scene.Node.queryAccessibleAttribute.

optional list of parameters

Returns:Object

Doc from javafx.scene.Node.queryAccessibleAttribute.

the value for the requested attribute

Annotations
@Override
selectionModelPropertyback to summary
public final ObjectProperty<SingleSelectionModel<T>> selectionModelProperty()
setButtonCellback to summary
public final void setButtonCell(ListCell<T> value)
setCellFactoryback to summary
public final void setCellFactory(Callback<ListView<T>, ListCell<T>> value)
setConverterback to summary
public final void setConverter(StringConverter<T> value)
setItemsback to summary
public final void setItems(ObservableList<T> value)
setPlaceholderback to summary
public final void setPlaceholder(Node value)
setSelectionModelback to summary
public final void setSelectionModel(SingleSelectionModel<T> value)
setVisibleRowCountback to summary
public final void setVisibleRowCount(int value)
updateValueback to summary
private void updateValue(T newValue)
visibleRowCountPropertyback to summary
public final IntegerProperty visibleRowCountProperty()
javafx.scene.control back to summary

pack-priv Class ComboBox.ComboBoxSelectionModel<T>

extends SingleSelectionModel<T>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private boolean
private final ComboBox<T>
private final ListChangeListener<T>
private final InvalidationListener
private WeakListChangeListener<T>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
protected int
getItemCount()

Implements abstract javafx.scene.control.SingleSelectionModel.getItemCount.

Gets the number of items available for the selection model.

protected T
getModelItem(int
The position of the item in the underlying data model.
index
)

Implements abstract javafx.scene.control.SingleSelectionModel.getModelItem.

Gets the data model item associated with a specific index.

private void
Inherited from javafx.scene.control.SingleSelectionModel:
clearAndSelectclearSelectionclearSelectionisEmptyisSelectedselectselectselectFirstselectLastselectNextselectPrevious

Field Detail

atomicback to summary
private boolean atomic
comboBoxback to summary
private final ComboBox<T> comboBox
itemsContentObserverback to summary
private final ListChangeListener<T> itemsContentObserver
itemsObserverback to summary
private final InvalidationListener itemsObserver
weakItemsContentObserverback to summary
private WeakListChangeListener<T> weakItemsContentObserver

Constructor Detail

ComboBoxSelectionModelback to summary
public ComboBoxSelectionModel(final ComboBox<T> cb)

Method Detail

doAtomicback to summary
private void doAtomic(Runnable r)
getItemCountback to summary
protected int getItemCount()

Implements abstract javafx.scene.control.SingleSelectionModel.getItemCount.

Doc from javafx.scene.control.SingleSelectionModel.getItemCount.

Gets the number of items available for the selection model. If the number of items can change dynamically, it is the responsibility of the concrete SingleSelectionModel implementation to ensure that items are selected or unselected as appropriate as the items change.

Returns:int

A number greater than or equal to 0.

Annotations
@Override
getModelItemback to summary
protected T getModelItem(int index)

Implements abstract javafx.scene.control.SingleSelectionModel.getModelItem.

Doc from javafx.scene.control.SingleSelectionModel.getModelItem.

Gets the data model item associated with a specific index.

Parameters
index:int

The position of the item in the underlying data model.

Returns:T

The item that exists at the given index.

Annotations
@Override
updateItemsObserverback to summary
private void updateItemsObserver(ObservableList<T> oldList, ObservableList<T> newList)