5

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)

Gundon
  • 2,081
  • 6
  • 28
  • 46

2 Answers2

5

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks for your reply! This works well. So there is no way of doing it via the 'normal' FXML-events? If you could tell me where I might have been able to lookup the existing (and defineable) events for FXML-files, I mark your answer as answering my question. – Gundon Aug 11 '12 at 22:40
  • No, you can't lookup events for this in FXML using JavaFX 2.0 to 2.2. – jewelsea Aug 11 '12 at 22:52
  • The selection model event triggers even when the value of the choicebox is set by code (in my case, I have one choice which populates a form of child choiceboxes) and I wish there was a clean way to distinguish "user driven changes" from code driven ones. – Mikeb Jul 28 '15 at 16:39
0

You can also use FXML onAction attribute:

<ChoiceBox onAction="#languageSelectionModified" />
Saeid Nourian
  • 1,606
  • 15
  • 32