What is the simple way to check whether any item is selected in JavaFX ComboBox or if it is left without selected item?
Checking for ComboBox in this state should return FALSE:
Checking for ComboBox in this state should return TRUE:
You can use JComboBox#getSelectedIndex , which will return -1 if nothing is selected or JComboBox#getSelectedItem which will return null if nothing is selected.
The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only. Save this answer.
JavaFX ComboBox is an implementation of simple ComboBox which shows a list of items out of which user can select at most one item, it inherits the class ComboBoxBase. Constructors of ComboBox: ComboBox(): creates a default empty combo box. ComboBox(ObservableList i): creates a combo box with the given items.
You can't add items to combobox through SceneBuilder. Either you can add through FXML file as you did or through controller as given below. I suggest calling: comboBox. getItems().
You can use
boolean isMyComboBoxEmpty = myComboBox.getSelectionModel().isEmpty();
Which also works, if you have a null
item among the ComboBox
items.
So I found a simple way:
boolean isMyComboBoxEmpty = (myComboBox.getValue() == null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With