JAVAFX button action to call a function of other class


Question

I am creating a project using javafx. I am using netbeans IDE. I had created many classes. When a button is pressed, a function from other class has to be worked. How to make it working ?

1
2
1/14/2014 2:59:35 PM

Accepted Answer

With Java 8:

button.setOnAction(e -> anInstanceOfYourOtherClass.yourMethod());

Prior to Java 8:

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        anInstanceOfYourOtherClass.yourMethod();
    }
});
4
1/14/2014 2:59:25 PM

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