Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if any item is selected in JavaFX ComboBox

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: ComboBox in this state should return FALSE

Checking for ComboBox in this state should return TRUE: ComboBox in this state should return TRUE

like image 678
PeterB Avatar asked May 01 '16 05:05

PeterB


People also ask

How to check ComboBox selected value in Java?

You can use JComboBox#getSelectedIndex , which will return -1 if nothing is selected or JComboBox#getSelectedItem which will return null if nothing is selected.

What is the difference between ComboBox and ChoiceBox JavaFX?

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.

How to set ComboBox in JavaFX?

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.

How do you add items to a ComboBox in Scene Builder?

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().


2 Answers

You can use

boolean isMyComboBoxEmpty = myComboBox.getSelectionModel().isEmpty();

Which also works, if you have a null item among the ComboBox items.

like image 98
fabian Avatar answered Sep 21 '22 17:09

fabian


So I found a simple way:

boolean isMyComboBoxEmpty = (myComboBox.getValue() == null);
like image 37
PeterB Avatar answered Sep 17 '22 17:09

PeterB