jilt3d
jilt3d

Reputation: 4034

How to make JavaFX 2.0 ChoiceBox to select its first element

When I create a JavaFX 2.0 ChoiceBox instance like this:

final ChoiceBox<String> fontChBox = 
  new ChoiceBox<>(FXCollections.observableArrayList("First", "Second", "Third"));

a choice box is displayed with no selection. I would like to select the first element by default. How to do it in JavaFX 2.0?

Upvotes: 22

Views: 32356

Answers (3)

user7514504
user7514504

Reputation: 21

Try following solution

//use this to display first option.
mychoicebox.getSelectionModel().selectFirst();


//to display specific option
mychoicebox.getSelectionModel().selectFirst(index position);

Upvotes: 2

Hirosh Wickramasuriya
Hirosh Wickramasuriya

Reputation: 451

Try this,

fontChBox.getSelectionModel().select(0);

if you need to select an element other than the first, pass the index of the element instead of zero.

Upvotes: 2

Juvanis
Juvanis

Reputation: 25950

Give a try to this statement:

 fontChBox.getSelectionModel().selectFirst();

Upvotes: 49

Related Questions