Which ChoiceBox-Event to choose?


Question

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)

1
5
8/11/2012 10:16:05 PM

Accepted Answer

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.

5
8/11/2012 10:58:13 PM

You can also use FXML onAction attribute:

<ChoiceBox onAction="#languageSelectionModified" />

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon