When adding a JavaFX button by code, how can I call the .setOnAction
method of the button from another class.
For instance, if I was to handle the button press within the same class:
public class SomeClass{
Button continueButton = new Button("Continue");
continueButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
someMethod();
}
});
}
However if I wish to utilise a controller for this, how can 'link' the actionEvent to a method within the other class.
eg:
public class SomeClass{
private SomeClassController controller;
Button continueButton = new Button("Continue");
continueButton.setOnAction(
//Call continuePressed() on controller
);
}
public class SomeClassController{
public void continuePressed(){
someMethod();
}
}
See Question&Answers more detail:os