Reputation: 1002
I've attempted to set the default selected item of my choicebox, however it isn't working as intended...
<ChoiceBox fx:id="d" value="- Select choice -">
<String fx:value="hellow"/>
</ChoiceBox>
Upvotes: 1
Views: 29003
Reputation: 11
This question was asked many years ago but I didn't like the answers given. I was recently having the same issue and I finally realized what the problem was. Take this for example:
<ChoiceBox fx:id="cb_DBEditors" layoutX="148.0" layoutY="192.0" prefHeight="25.0" prefWidth="124.0" value="Testing">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="SQL Plus" />
<String fx:value="Something Else" />
</FXCollections>
</items>
</ChoiceBox>
The value you have in the default value=""
must equal one of the fx:value
strings. If it isn't then the ChoiceBox
will be blank. To recap, the default value you set must be one of the fx:value
in your <items>
list.
<ChoiceBox fx:id="cb_DBEditors" layoutX="148.0" layoutY="192.0" prefHeight="25.0" prefWidth="124.0" value="SQL Plus">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="SQL Plus" />
<String fx:value="Something Else" />
</FXCollections>
</items>
</ChoiceBox>
Upvotes: 1
Reputation: 1
When we use 'value' then it will set default value for your choice box or even you can display any short message for your choice box:
<ChoiceBox value="- Select choice -">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="first choice"></String>
</FXCollections>
</items>*emphasized text*
</ChoiceBox>
Upvotes: 0
Reputation: 107
You can use .setValue("");
for setting the default value.. point to note the valuename
should be present in observablearray("","","")
Example
@fxml
private ChoiceBox choiceId; // this is fxml choicebox Id name given in fxml file
ObservableList<String> options = FXCollections.observableArrayList("valuename1","valuename2");
choiceId.setValue("valuename1"); // this statement shows default value
choiceId.setItems(options); // this statement adds all values in choiceBox
Upvotes: 5
Reputation: 80
This answer is answered in the question JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?
For example when you want to select the second value as default value you can do following in your FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="choicebox.defaultselection.FXMLDocumentController">
<children>
<ChoiceBox layoutX="16.0" layoutY="52.0" prefWidth="150.0" value="5 minutes">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2 minutes" />
<String fx:value="5 minutes" />
<String fx:value="15 minutes" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
Upvotes: 5