Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JavaFX 2.0 ChoiceBox to select its first element

Tags:

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?

like image 703
jilt3d Avatar asked Mar 07 '12 16:03

jilt3d


People also ask

What is the difference between ComboBox and ChoiceBox JavaFX?

The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only.

What is a choice box?

The ChoiceBox is used for presenting the user with a relatively small set of predefined choices from which they may choose. The ChoiceBox, when "showing", will display to the user these choices and allow them to pick exactly one choice. When not showing, the current choice is displayed.

What is JavaFX ChoiceBox?

ChoiceBox is a part of the JavaFX package. ChoiceBox shows a set of items and allows the user to select a single choice and it will show the currently selected item on the top. ChoiceBox by default has no selected item unless otherwise selected.


2 Answers

Give a try to this statement:

 fontChBox.getSelectionModel().selectFirst();
like image 52
Juvanis Avatar answered Oct 03 '22 21:10

Juvanis


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.

like image 33
Hirosh Wickramasuriya Avatar answered Oct 03 '22 21:10

Hirosh Wickramasuriya