2

There is a way how to set default value to ComboBox on JavaFx fxml.

I found sulution here: https://stackoverflow.com/a/14436371/1344424

<ComboBox editable="true">
    <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>

But it not working when Editable property set to true. How can I set default value to editable ComboBox?

Community
  • 1
  • 1
picoworm
  • 310
  • 3
  • 13

2 Answers2

3

It seems to work if you set the value via an attribute, instead of an element:

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

</ComboBox>

It also works this way if you pass in a reference to the same string:

<ComboBox editable="true" value="$defaultSelection">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:id="defaultSelection" fx:value="NVT" />
            <String fx:value="Bezig" />
            <String fx:value="Positief" />
            <String fx:value="Negatief" />
        </FXCollections>
    </items>

</ComboBox>
James_D
  • 201,275
  • 16
  • 291
  • 322
  • 1
    All works! Interesting thing. When I put `value="$defaultSelection"` before `editable="true"`, nothing works. – picoworm Jan 29 '15 at 18:58
1

I fear this is not possible with FXML, but in Java code:

ComboBox<String> combobox = new ComboBox<>(FXCollections.observableArrayList("1","2","3","4","5"));
combobox.setEditable(true);
combobox.getSelectionModel().selectFirst();

Or if you want to select a specific value:

combobox.getSelectionModel().select("3");
eckig
  • 10,964
  • 4
  • 38
  • 52