title: How to communicate stage between main class and controller class in FXML application
body: <p>I want to communicate stage between main class and controller class so I can make my stage draggable from the code in the controller class. What is the most effecient way to do this?</p>
  
  <p>Main.java:</p>
  
  <pre><code>package sample;
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  import java.io.FileReader;
  import java.util.List;
  import java.util.Map;
  
  import javafx.stage.StageStyle;
  import org.json.simple.JSONArray;
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  
  public class Main extends Application {
  
    public Stage stage;
    @Override
    public void start(Stage primaryStage) throws Exception{
        stage = primaryStage;
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle("WordToday");
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.setX(5);
        stage.show();
  
        //FileReader reader = new       FileReader("C:\\Users\\Snipester\\IdeaProjects\\WordToday\\src\\sample\\Words.json");
        //JSONObject jsonObj = (JSONObject) new JSONParser().parse(reader);
        //JSONArray jsonArray = (JSONArray) jsonObj.get("Gre");
        //List&lt;Map&lt;String, String&gt;&gt; list = jsonArray;
        //System.out.println(list.get(0).get("word"));
  
    }
  
  
    public static void main(String[] args) {
        launch(args);
    }
  }
  </code></pre>
  
  <p>Controller.java:</p>
  
  <pre><code>package sample;
  
  import javafx.application.Platform;
  import javafx.fxml.FXML;
  import javafx.scene.control.Button;
  import javafx.scene.control.ToolBar;
  import javafx.scene.input.MouseEvent;
  import javafx.stage.Stage;
  
  public class Controller{
  
  
      public Button exitButton;
      public ToolBar titleBar;
      private double x, y;
  
  
      //exit button
      public void exitButtonClicked() {
          Platform.exit();
      }
  
      //mouse pressed
      public void MouseClicked(MouseEvent event){
          //x = event.getSceneX();
          //y = event.getSceneY();
      }
  
      //mouse dragged
      public void MouseDragged(MouseEvent event){
         // stage.setX(event.getScreenX()-x);
          //stage.setY(event.getScreenY()-y);
  
      }
  
  }
  </code></pre>
  
  <p>FXML:</p>
  
  <pre><code> &lt;ToolBar fx:id="titleBar"
             prefHeight="20.0"
             prefWidth="600.0"
             GridPane.rowIndex="0"
             onMouseClicked="#MouseClicked"
             onMouseDragged="#MouseDragged"
             /&gt;
  </code></pre>