Search code examples
javajavafxcombobox

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


Solution

  • You can use

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

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