Class ComboBox<T>

Type Parameters:
T - The type of the value that has been selected or otherwise entered in to this ComboBox
All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class ComboBox<T> extends ComboBoxBase<T>
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 ComboBoxBase.valueProperty() from 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 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.

A warning about inserting Nodes 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 not recommended. 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.

The recommended approach, rather than inserting Node instances into the items list, is to put the relevant information into the ComboBox, and then provide a custom cell factory. For example, rather than use the following code:

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

You should do the following:

 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);
             }
         }
     };
 });
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:
  • Property Details

  • Constructor Details

    • ComboBox

      public ComboBox()
      Creates a default ComboBox instance with an empty items list and default selection model.
    • ComboBox

      public ComboBox(ObservableList<T> items)
      Creates a default ComboBox instance with the provided items list and a default selection model.
      Parameters:
      items - the list of items
  • Method Details

    • setItems

      public final void setItems(ObservableList<T> value)
      Sets the value of the items property.
      Property description:
      The list of items to show within the ComboBox popup.
      Parameters:
      value - the value for the items property
      See Also:
    • getItems

      public final ObservableList<T> getItems()
      Gets the value of the items property.
      Property description:
      The list of items to show within the ComboBox popup.
      Returns:
      the value of the items property
      See Also:
    • itemsProperty

      public ObjectProperty<ObservableList<T>> itemsProperty()
      The list of items to show within the ComboBox popup.
      Returns:
      the items property
      See Also:
    • converterProperty

      public 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:
      the converter property
      See Also:
    • setConverter

      public final void setConverter(StringConverter<T> value)
      Sets the value of the converter property.
      Property description:
      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.
      Parameters:
      value - the value for the converter property
      See Also:
    • getConverter

      public final StringConverter<T> getConverter()
      Gets the value of the converter property.
      Property description:
      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:
      the value of the converter property
      See Also:
    • setCellFactory

      public final void setCellFactory(Callback<ListView<T>,ListCell<T>> value)
      Sets the value of the cellFactory property.
      Property description:
      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.
      Parameters:
      value - the value for the cellFactory property
      See Also:
    • getCellFactory

      public final Callback<ListView<T>,ListCell<T>> getCellFactory()
      Gets the value of the cellFactory property.
      Property description:
      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.
      Returns:
      the value of the cellFactory property
      See Also:
    • cellFactoryProperty

      public ObjectProperty<Callback<ListView<T>,ListCell<T>>> cellFactoryProperty()
      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.
      Returns:
      the cellFactory property
      See Also:
    • buttonCellProperty

      public 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:
      the button cell property
      Since:
      JavaFX 2.2
      See Also:
    • setButtonCell

      public final void setButtonCell(ListCell<T> value)
      Sets the value of the buttonCell property.
      Property description:
      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.
      Parameters:
      value - the value for the buttonCell property
      Since:
      JavaFX 2.2
      See Also:
    • getButtonCell

      public final ListCell<T> getButtonCell()
      Gets the value of the buttonCell property.
      Property description:
      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:
      the value of the buttonCell property
      Since:
      JavaFX 2.2
      See Also:
    • setSelectionModel

      public final void setSelectionModel(SingleSelectionModel<T> value)
      Sets the value of the selectionModel property.
      Property description:
      The selection model for the ComboBox. A ComboBox only supports single selection.
      Parameters:
      value - the value for the selectionModel property
      See Also:
    • getSelectionModel

      public final SingleSelectionModel<T> getSelectionModel()
      Gets the value of the selectionModel property.
      Property description:
      The selection model for the ComboBox. A ComboBox only supports single selection.
      Returns:
      the value of the selectionModel property
      See Also:
    • selectionModelProperty

      public final ObjectProperty<SingleSelectionModel<T>> selectionModelProperty()
      The selection model for the ComboBox. A ComboBox only supports single selection.
      Returns:
      the selectionModel property
      See Also:
    • setVisibleRowCount

      public final void setVisibleRowCount(int value)
      Sets the value of the visibleRowCount property.
      Property description:
      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.
      Parameters:
      value - the value for the visibleRowCount property
      See Also:
    • getVisibleRowCount

      public final int getVisibleRowCount()
      Gets the value of the visibleRowCount property.
      Property description:
      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.
      Returns:
      the value of the visibleRowCount property
      See Also:
    • visibleRowCountProperty

      public final IntegerProperty visibleRowCountProperty()
      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.
      Returns:
      the visibleRowCount property
      See Also:
    • getEditor

      public final TextField getEditor()
      Gets the value of the editor property.
      Property description:
      The editor for the ComboBox. The editor is null if the ComboBox is not editable.
      Returns:
      the value of the editor property
      Since:
      JavaFX 2.2
      See Also:
    • editorProperty

      public final ReadOnlyObjectProperty<TextField> editorProperty()
      The editor for the ComboBox. The editor is null if the ComboBox is not editable.
      Returns:
      the editor property
      Since:
      JavaFX 2.2
      See Also:
    • placeholderProperty

      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:
      the placeholder property
      Since:
      JavaFX 8.0
      See Also:
    • setPlaceholder

      public final void setPlaceholder(Node value)
      Sets the value of the placeholder property.
      Property description:
      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.
      Parameters:
      value - the value for the placeholder property
      Since:
      JavaFX 8.0
      See Also:
    • getPlaceholder

      public final Node getPlaceholder()
      Gets the value of the placeholder property.
      Property description:
      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:
      the value of the placeholder property
      Since:
      JavaFX 8.0
      See Also:
    • commitValue

      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
    • cancelEdit

      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