1

According to the JavaFX tutorial it should be possible to register event handlers to observable properties in FXML:

Any class that defines a setOnEvent() method can be assigned an event handler in markup, as can any observable property (via an "onPropertyChange" attribute).

Now, I'm trying to register an event handler for the selected property of ToggleButton:

<ToggleButton text="%SomePane.fooButton.text" onSelectedChanged="#handleFooSelectedChanged" toggleGroup="$toggleGroup"/>

and in the controller:

@FXML
public void handleFooSelectedChanged(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

}

But I'm getting the following error:

Caused by: javafx.fxml.LoadException: Controller method "handleFooSelectedChanged" not found.

Do I have to change the method signature? Is this a bug? Or is this not supported at all?

Puce
  • 37,247
  • 13
  • 80
  • 152

1 Answers1

2

Your FXML-attribute is wrong! The pattern is on<PropertyName>Change (without 'd'), not on<PropertyName>Changed!

So this should work: onSelectedChange="#handleFooSelectedChanged"

Note: Your controller method can also look like this:

@FXML
public void handleFooSelectedChanged(BooleanProperty observable, boolean oldValue, boolean newValue);
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • I really missed that 'd'. Thanks for pointing this out! Currently it works, but only with no method arguments. Neither the one I suggested (which is the same the one needed by `ChangeListener` nor the one suggested by you (with BooleanProperty and boolean primitives) works!? – Puce Nov 27 '13 at 13:22
  • Hmm, from the FXMLLoader source code of JavaFX 8, both variants should work. Can you confirm this also works with JavaFX 2.x? – Puce Nov 27 '13 at 13:37
  • No, sorry, I'm using JavaFX 8. Don't know if it should work with 2.2 too! – isnot2bad Nov 27 '13 at 14:23
  • @Puce JavaFX 8 has many improvements over JavaFX 2.2. Is it possible for you to migrate? – isnot2bad Nov 27 '13 at 14:25