Reputation: 310
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?
Upvotes: 2
Views: 2919
Reputation: 209235
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>
Upvotes: 3
Reputation: 11134
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");
Upvotes: 1