Philayyy Philayyy - 2 months ago 12
Java Question

Is there a way to check if a comboBox has any items in javaFX?

Is there a way to check if a comboBox has any items in it or whether it is empty? I have an array of combo boxes and I need to go through each of them, if there are no items in the combo box, then I must hide it. This following code doesn't seem to work:

for (ComboBox cmb : comboBoxes) {
if (cmb.getItems().isEmpty()) {
cmb.hide();
}
}

Answer

The .getItems() method returns an ObservableList<T> so you can just check its .size(). This will tell you if it's empty.

for (ComboBox cmb : comboBoxes) {
  if (cmb.getItems().size() < 0) { cmb.setVisible(false); }
}

If the ComboBox is populated by a List of its own, you could also just check if the list is empty with the same .size() call.

Comments