Search code examples
javafx-2javafxfxml

JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?


I have the following FXML:

<ChoiceBox>
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="2 minutes" />
            <String fx:value="5 minutes" />
            <String fx:value="15 minutes" />
        </FXCollections>
    </items>
</ChoiceBox>

But in the GUI it just shows a ChoiceBox with a default of nothing. I would like the first element in the list to be the default, and for a choice of "null" or nothing to be prohibited.

How do I accomplish this?


Solution

  • I added the value attribute to the ChoiceBox tag, and that worked.

    <ChoiceBox value="2 minutes">
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value="2 minutes" />
                <String fx:value="5 minutes" />
                <String fx:value="15 minutes" />
            </FXCollections>
        </items>
    </ChoiceBox>