Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which ChoiceBox-Event to choose?

I placed a ChoiceBox inside an fxml with JavaFX Scene Builder.

The FXML has a controller assigned to it.

My question is: Which event do I need to register if I want to know about changed values?

onInputMethodTextChanged="#languageSelectionModified"

this does not work with the following code

public void languageSelectionModified(Event event) {
    ChoiceBox<String> box = (ChoiceBox<String>) event.getSource();
    System.out.println(box.getValue());
}

and this only works for the initial click (i.e. opening the list, not when selecting an item):

onMouseClicked="#languageSelectionModified"

Although the Mouse-Events would never be a good choise because of situations where the touch or keyboard is the input-method, it still proves that the System.out can be reached.

I have absolutly no idea where those things are documentated (in the default Java-API they are not)

like image 491
Gundon Avatar asked Aug 11 '12 22:08

Gundon


People also ask

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.


1 Answers

Add a listener to your @FXML injected choicebox in your controller:

choicebox.getSelectionModel().selectedItemProperty().addListener(choiceboxSelectionChangeListener);

You can also bind to the selected item:

label.textProperty().bind(choicebox.getSelectionModel().selectedItemProperty());

Here is an example of hooking up a listener in a controller for a ComboBox defined in FXML. Logic for a ChoiceBox is pretty much identical.

like image 119
jewelsea Avatar answered Sep 28 '22 07:09

jewelsea