title: Can I call another class from the setOnAction parameter in javaFX?
body: <p>I am a beginner programmer and I learned enough of java, now my goal is to create a calculator using JavaFX for gui and have it as oop as possible so that in practice, it is easy for someone else to modify the program, I have encountered a problem when adding a button and wanting to apply the <code>setOnAction</code> method to it, I want to have a separate class for handling the events of buttons but I can't understand how to do that with this particular method </p>
  
  <p>This is the <code>Gui</code> class:</p>
  
  <pre><code>package calculator;
  
  import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.StackPane;
  import javafx.stage.Stage;
  
  public class Gui extends Application {
  
      InputHandler inputhandler = new InputHandler();
  
      Button add = new Button();
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          primaryStage.setTitle("HackCalc");
  
  
          add.setText("+");
          add.setOnAction(inputhandler);
  
  
          StackPane layout = new StackPane();
          layout.getChildren().add(add);
  
          Scene scene = new Scene(layout, 330, 500);
  
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  }
  </code></pre>
  
  <p>As you can see, I have gone as far as creating an instance of the <code>InputHandler</code> class which I will show you and passing it in <code>setOnAction</code> but it never worked, I had a frustrating <code>NoSuchMethodException</code> error.</p>
  
  <p>Now, here's the <code>InputManager</code> class:</p>
  
  <pre><code>import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  
  public  class InputHandler extends Gui implements EventHandler&lt;ActionEvent&gt;{
  
      @Override
      public void handle(ActionEvent event) {
          if(event.getSource()== add){
              System.out.println("InputHandler works");
          }
      }
  }
  </code></pre>