…the only parameter allowed in the setOnAction()
method is the event.
No: the sole parameter to the setOnAction()
method is an EventHandler
, a functional interface defining a method that will be invoked whenever the button is fired. That method, named handle()
, will receive an ActionEvent
as its parameter when called.
Is there a way to pass additional parameters to the setOnAction
method?
Yes: given a method with parameters such as this:
private void changeSceneOnBtnClick(ActionEvent event,
String fileName, TextField playerName) {
…
}
You can tell the button to invoke that method with parameters when clicked like this:
button.setOnAction((event) -> changeSceneOnBtnClick(
event, "stage1.fxml", new TextField("Player 1")));
As outlined here, this is shorthand for this:
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
changeSceneOnBtnClick(
event, "stage1.fxml", new TextField("Player 1"));
}
});
Maybe: although I haven't seen a use-case for it, you can also aggregate attributes in your own EventHandler
and use it like this:
button.setOnAction(new MyEventHandler("stage1.fxml", new TextField("Player 1")));
…
private static final class MyEventHandler implements EventHandler<ActionEvent> {
private final String fileName;
private final TextField playerName;
public MyEventHandler(String fileName, TextField playerName) {
this.fileName = fileName;
this.playerName = playerName;
}
@Override
public void handle(ActionEvent event) {
…
}
}
Definitely: review your design critically, knowing that many schemes end up in the trash. Search for designs that meet your goals: for example, study this approach to adding nodes dynamically.