FXML - How to preselect an item in ChoiceBox?

1k views Asked by At

What is the FXML equivalent for this (cb is a ChoiceBox):

cb.getSelectionModel().select(1);

I tried something like this:

<ChoiceBox SelectionModel.select="1">
....
</ChoiceBox>

and

<ChoiceBox>
<SelectionModel select="1"/>
....
</ChoiceBox>

and a lot of similar variations, but those didn't work

2

There are 2 answers

0
M.Onirban On

if you using scene builder then try this in fxml.

<ChoiceBox fx:id="choice" layoutX="181.0" layoutY="14.0" prefHeight="25.0" prefWidth="119.0" value="item1" >
<String fx:value="item1" />
<Separator />
<String fx:value="item2" />
</ChoiceBox>
0
JimClarke On

Right now, you have to do it via Java code in the Controller class. There is a pending enhancement request already submitted to handle this in the FXML. It looks very similar to your posting here.