ComboBox FXML default value

Perneel picture Perneel · Jul 27, 2012 · Viewed 36k times · Source

How can I set a default value in a ComboBox using FXML?

<ComboBox fx:id="cbo_Bacteriologie_Aesculine" prefHeight="21.0" prefWidth="105.0" GridPane.columnIndex="1" GridPane.rowIndex="0">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="NVT" />
            <String fx:value="Bezig" />
            <String fx:value="Positief" />
            <String fx:value="Negatief" />
        </FXCollections>
    </items>
</ComboBox>

I want NVT to be selected by default. I tried adding selected="selected" and such but don't seem to find the right syntax.

Is it possible to edit the listed items using Scene Builder? I can't seem to find it.

Answer

Guedolino picture Guedolino · Jan 21, 2013

Use this:

<ComboBox>
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="NVT" />
            <String fx:value="Bezig" />
            <String fx:value="Positief" />
            <String fx:value="Negatief" />
        </FXCollections>
    </items>
    <value>
        <String fx:value="NVT" />
    </value>
</ComboBox>