Date Published:
Last Modified:

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:

public enum MyEnum {

    OPTION_1("Option 1"),
    OPTION_2("Option 2"),
    OPTION_3("Option 3"),

    private String label;

    MyEnum(String label) {
        this.label = label;

    public String toString() {
        return label;


Then, to populate the ComboBox, just type:


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


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

Related Content:


comments powered by Disqus