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

jononomo picture jononomo · Aug 15, 2013 · Viewed 23.7k times · Source

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?

Answer

jononomo picture jononomo · Aug 15, 2013

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>