JAVAFX

# ComboBoxes

Article by:

## Associating With An Enum

One of the tidiest ways of populating a JavaFX ComboBox is to associate it with an enumeration. The enumeration defines the objects you can select from in the ComboBox, as well as how to display these objects (their String representations).

First, we need to create an enum:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  public enum MyEnum { OPTION_1("Option 1"), OPTION_2("Option 2"), OPTION_3("Option 3"), ; private String label; MyEnum(String label) { this.label = label; } @Override public String toString() { return label; } } 

Then, to populate the ComboBox, just type:

 1  myComboBox.getItems().setAll(MyEnum.values()); 

To select an item from code (useful for say, setting the default value of the ComboBox), type:

 1  myComboBox.getSelectionModel().select(MyEnum.OPTION_1); 

Notice we are now dealing with objects, rather than having to parse strings!

## Authors

### Geoffrey Hunter

Dude making stuff.