By default, the ChoiceBox has no item selected unless otherwise specified. Although the ChoiceBox will only allow a user to select from the predefined list, it is possible for the developer to specify the selected item to be something other than what is available in the predefined list. This is required for several important use cases. Configuration of the ChoiceBox is order independent. You may either specify the items and then the selected item, or you may specify the selected item and then the items. Either way will function correctly.
ChoiceBox item selection is handled by
SelectionModel.
As with ListView and ComboBox, it is possible to modify the
SelectionModel that is used,
although this is likely to be rarely changed. ChoiceBox supports only a
single selection model, hence the default used is a SingleSelectionModel.
Example:
ChoiceBox cb = new ChoiceBox();
cb.getItems().addAll("item1", "item2", "item3");
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private final ObjectProperty | |
| private ObjectProperty | items
The items to display in the choice box. |
| private final ListChangeListener | |
| public static final EventType | ON_HIDDEN
Called when the ChoiceBox popup has been hidden. |
| public static final EventType | ON_HIDING
Called when the ChoiceBox popup will be hidden. |
| public static final EventType | ON_SHOWING
Called prior to the ChoiceBox showing its popup after the user has clicked or otherwise interacted with the ChoiceBox. |
| public static final EventType | ON_SHOWN
Called after the ChoiceBox has shown its popup. |
| private ObjectProperty | |
| private ObjectProperty | |
| private ObjectProperty | |
| private ObjectProperty | |
| private ObjectProperty | |
| private ChangeListener | |
| private ObjectProperty | selectionModel
The selection model for the ChoiceBox. |
| private ReadOnlyBooleanWrapper | showing
Indicates whether the drop down is displaying the list of choices to the user. |
| private static final PseudoClass | |
| private final ObjectProperty |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ChoiceBox(ObservableList<T>
the set of items items)Create a new ChoiceBox with the given set of items. |
| Modifier and Type | Method and Description |
|---|---|
| public final ObjectProperty | Returns: the string converter propertyAllows a way to specify how to represent objects in the items list. |
| protected Skin | createDefaultSkin()
Overrides javafx. Create a new instance of the default skin for this control. |
| public void | executeAccessibleAction(AccessibleAction
the action to execute action, Object... optional list of parameters parameters)Overrides javafx. This method is called by the assistive technology to request the action indicated by the argument should be executed. |
| public final StringConverter | |
| public final ObservableList | |
| public final EventHandler | |
| public final EventHandler | |
| public final EventHandler | |
| public final EventHandler | |
| public final EventHandler | |
| public final SingleSelectionModel | |
| public final T | |
| public void | |
| public final boolean | |
| public final ObjectProperty | |
| public final ObjectProperty | Returns: the on action propertyThe ChoiceBox action, which is invoked whenever the ChoiceBox
|
| public final ObjectProperty | Returns: the on hidden propertyCalled just after the |
| public final ObjectProperty | Returns: the on hiding propertyCalled just prior to the |
| public final ObjectProperty | Returns: the on showing propertyCalled just prior to the |
| public final ObjectProperty | |
| public Object | queryAccessibleAttribute(AccessibleAttribute
the requested attribute attribute, Object... optional list of parameters parameters)Overrides javafx. This method is called by the assistive technology to request the value for an attribute. |
| public final ObjectProperty | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| private void | |
| public final void | |
| public void | |
| public final ReadOnlyBooleanProperty | |
| public final ObjectProperty | Returns: the value propertyThe value of this ChoiceBox is defined as the selected item in the ChoiceBox selection model. |
| converter | back to summary |
|---|---|
| private final ObjectProperty<StringConverter<T>> converter | |
| items | back to summary |
|---|---|
| private ObjectProperty<ObservableList<T>> items The items to display in the choice box. The selected item (as indicated in the selection model) must always be one of these items. | |
| itemsListener | back to summary |
|---|---|
| private final ListChangeListener<T> itemsListener | |
| ON_HIDDEN | back to summary |
|---|---|
| public static final EventType<Event> ON_HIDDEN Called when the ChoiceBox popup has been hidden.
| |
| ON_HIDING | back to summary |
|---|---|
| public static final EventType<Event> ON_HIDING Called when the ChoiceBox popup will be hidden.
| |
| ON_SHOWING | back to summary |
|---|---|
| public static final EventType<Event> ON_SHOWING Called prior to the ChoiceBox showing its popup after the user has clicked or otherwise interacted with the ChoiceBox.
| |
| ON_SHOWN | back to summary |
|---|---|
| public static final EventType<Event> ON_SHOWN Called after the ChoiceBox has shown its popup.
| |
| onAction | back to summary |
|---|---|
| private ObjectProperty<EventHandler<ActionEvent>> onAction | |
| onHidden | back to summary |
|---|---|
| private ObjectProperty<EventHandler<Event>> onHidden | |
| onHiding | back to summary |
|---|---|
| private ObjectProperty<EventHandler<Event>> onHiding | |
| onShowing | back to summary |
|---|---|
| private ObjectProperty<EventHandler<Event>> onShowing | |
| onShown | back to summary |
|---|---|
| private ObjectProperty<EventHandler<Event>> onShown | |
| selectedItemListener | back to summary |
|---|---|
| private ChangeListener<T> selectedItemListener | |
| selectionModel | back to summary |
|---|---|
| private ObjectProperty<SingleSelectionModel<T>> selectionModel The selection model for the ChoiceBox. Only a single choice can be made, hence, the ChoiceBox supports only a SingleSelectionModel. Generally, the main interaction with the selection model is to explicitly set which item in the items list should be selected, or to listen to changes in the selection to know which item has been chosen. | |
| showing | back to summary |
|---|---|
| private ReadOnlyBooleanWrapper showing Indicates whether the drop down is displaying the list of choices to the user. This is a readonly property which should be manipulated by means of the #show and #hide methods. | |
| SHOWING_PSEUDOCLASS_STATE | back to summary |
|---|---|
| private static final PseudoClass SHOWING_PSEUDOCLASS_STATE | |
| value | back to summary |
|---|---|
| private final ObjectProperty<T> value | |
| ChoiceBox | back to summary |
|---|---|
| public ChoiceBox() Create a new ChoiceBox which has an empty list of items. | |
| ChoiceBox | back to summary |
|---|---|
| public ChoiceBox(ObservableList<T> items) Create a new ChoiceBox with the given set of items. Since it is observable, the content of this list may change over time and the ChoiceBox will be updated accordingly.
| |
| converterProperty | back to summary |
|---|---|
| public final ObjectProperty Allows a way to specify how to represent objects in the items list. When
a StringConverter is set, the object toString method is not called and
instead its toString(object T) is called, passing the objects in the items list.
This is useful when using domain objects in a ChoiceBox as this property
allows for customization of the representation. Also, any of the pre-built
Converters available in the
| |
| createDefaultSkin | back to summary |
|---|---|
| protected Skin Overrides javafx. Doc from javafx. 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 | |
| executeAccessibleAction | back to summary |
|---|---|
| public void executeAccessibleAction(AccessibleAction action, Object... parameters) Overrides javafx. Doc from javafx. This method is called by the assistive technology to request the action indicated by the argument should be executed.
This method is commonly overridden by subclasses to implement
action that are required for a specific role.
| |
| getConverter | back to summary |
|---|---|
| public final StringConverter | |
| getItems | back to summary |
|---|---|
| public final ObservableList | |
| getOnAction | back to summary |
|---|---|
| public final EventHandler | |
| getOnHidden | back to summary |
|---|---|
| public final EventHandler | |
| getOnHiding | back to summary |
|---|---|
| public final EventHandler | |
| getOnShowing | back to summary |
|---|---|
| public final EventHandler | |
| getOnShown | back to summary |
|---|---|
| public final EventHandler | |
| getSelectionModel | back to summary |
|---|---|
| public final SingleSelectionModel | |
| getValue | back to summary |
|---|---|
| public final T getValue() | |
| hide | back to summary |
|---|---|
| public void hide() Closes the list of choices. | |
| isShowing | back to summary |
|---|---|
| public final boolean isShowing() | |
| itemsProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| onActionProperty | back to summary |
|---|---|
| public final ObjectProperty The ChoiceBox action, which is invoked whenever the ChoiceBox
| |
| onHiddenProperty | back to summary |
|---|---|
| public final ObjectProperty Called just after the
| |
| onHidingProperty | back to summary |
|---|---|
| public final ObjectProperty Called just prior to the
| |
| onShowingProperty | back to summary |
|---|---|
| public final ObjectProperty Called just prior to the
| |
| onShownProperty | back to summary |
|---|---|
| public final ObjectProperty Called just after the
| |
| queryAccessibleAttribute | back to summary |
|---|---|
| public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters) Overrides javafx. Doc from javafx. 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.
| |
| selectionModelProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| setConverter | back to summary |
|---|---|
| public final void setConverter(StringConverter<T> value) | |
| setItems | back to summary |
|---|---|
| public final void setItems(ObservableList<T> value) | |
| setOnAction | back to summary |
|---|---|
| public final void setOnAction(EventHandler<ActionEvent> value) | |
| setOnHidden | back to summary |
|---|---|
| public final void setOnHidden(EventHandler<Event> value) | |
| setOnHiding | back to summary |
|---|---|
| public final void setOnHiding(EventHandler<Event> value) | |
| setOnShowing | back to summary |
|---|---|
| public final void setOnShowing(EventHandler<Event> value) | |
| setOnShown | back to summary |
|---|---|
| public final void setOnShown(EventHandler<Event> value) | |
| setSelectionModel | back to summary |
|---|---|
| public final void setSelectionModel(SingleSelectionModel<T> value) | |
| setShowing | back to summary |
|---|---|
| private void setShowing(boolean value) | |
| setValue | back to summary |
|---|---|
| public final void setValue(T value) | |
| show | back to summary |
|---|---|
| public void show() Opens the list of choices. | |
| showingProperty | back to summary |
|---|---|
| public final ReadOnlyBooleanProperty showingProperty() | |
| valueProperty | back to summary |
|---|---|
| public final ObjectProperty The value of this ChoiceBox is defined as the selected item in the ChoiceBox selection model. The valueProperty is synchronized with the selectedItem. This property allows for bi-directional binding of external properties to the ChoiceBox and updates the selection model accordingly.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final ChoiceBox | |
| private ListChangeListener | |
| private ChangeListener | |
| private WeakListChangeListener |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected int | getItemCount()
Implements abstract javafx. 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. Gets the data model item associated with a specific index. |
| public void | select(int
The position of the item to select in the selection model. index)Overrides javafx. Selects the given row. |
| public void | select(T
The object to attempt to select in the underlying data model. obj)Overrides javafx. Selects the index for the first instance of given object in the underlying data model. |
| public void | |
| public void | selectPrevious()
Overrides javafx. Selects the previous index. |
| choiceBox | back to summary |
|---|---|
| private final ChoiceBox<T> choiceBox | |
| itemsContentObserver | back to summary |
|---|---|
| private ListChangeListener<T> itemsContentObserver | |
| itemsObserver | back to summary |
|---|---|
| private ChangeListener<ObservableList<T>> itemsObserver | |
| weakItemsContentObserver | back to summary |
|---|---|
| private WeakListChangeListener<T> weakItemsContentObserver | |
| ChoiceBoxSelectionModel | back to summary |
|---|---|
| public ChoiceBoxSelectionModel(final ChoiceBox<T> cb) | |
| getItemCount | back to summary |
|---|---|
| protected int getItemCount() Implements abstract javafx. Doc from javafx. 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.
| |
| getModelItem | back to summary |
|---|---|
| protected T getModelItem(int index) Implements abstract javafx. Doc from javafx. Gets the data model item associated with a specific index.
| |
| select | back to summary |
|---|---|
| public void select(int index) Overrides javafx. Selects the given row. Since the SingleSelectionModel can only support having a single row selected at a time, this also causes any previously selected row to be unselected. This method is overridden here so that we can move past a Separator in a ChoiceBox and select the next valid menuitem.
| |
| select | back to summary |
|---|---|
| public void select(T obj) Overrides javafx. Doc from javafx. Selects the index for the first instance of given object in the underlying data model. Since the SingleSelectionModel can only support having a single index selected at a time, this also causes any previously selected index to be unselected.
Overridden to clear
| |
| selectNext | back to summary |
|---|---|
| public void selectNext() Overrides javafx. Doc from javafx. Selects the next index. Since the SingleSelectionModel can only support having a single index selected at a time, this also causes any previously selected index to be unselected.
| |
| selectPrevious | back to summary |
|---|---|
| public void selectPrevious() Overrides javafx. Doc from javafx. Selects the previous index. Since the SingleSelectionModel can only support having a single index selected at a time, this also causes any previously selected index to be unselected.
| |