title: javafx - Separate EventHandler and gui code
body: <p>So i would like to know how i can separate Events in a class and have the GUI code in another class without them having direct knowledge of eachother.
  I have recently only been working in one class , where i have been putting GUI code and listeners in the start method. </p>
  
  <p>this is what iv'e done so far but the button dosen't work:</p>
  
  <p>GUI class:</p>
  
  <pre><code>package application;
  
  import javafx.application.Application;
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.BorderPane;
  
  
  public class GUI extends Application {
  
  Button btn1 = new Button("press me");
  
  @Override
  public void start(Stage primaryStage) {
      try {
          BorderPane root = new BorderPane();
          Scene scene = new Scene(root,400,400);
  
          root.setCenter(btn1);
          primaryStage.setScene(scene);
          primaryStage.show();
  
  
         } catch(Exception e) {
  
          e.printStackTrace();
          }
      }
  
  
  }
  </code></pre>
  
  <p>Controller class:</p>
  
  <pre><code>package application;
  
  import javafx.event.Event;
  import javafx.event.EventHandler;
  
  public class Control implements EventHandler&lt;Event&gt;{
  
  GUI gui;
  
  public Control(GUI gui) {
      this.gui = gui;
  }
  
  @Override
  public void handle(Event event) {
      Object cmd = event.getSource();
      if(gui.btn1.equals(cmd)){
          System.out.println("your pressed btn1");
        }
     }
  
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>package application;
  
  public class Main extends GUI {
  
  public static void main(String[] args) {
      launch(args);
  
      GUI gui = new GUI();
      Control control = new Control(gui);
     }
  }
  </code></pre>