0

I have a TableView item that when clicked will launch a Toast. The first parameter is the primary stage from the Main application class.

tableViewPriority.setOnMouseClicked(event -> {
    if (tableViewPriority.getSelectionModel().getSelectedItem() != null) {
        Toast.makeText(primaryStage, "Toast!", 0, 1, 1);
    }
 });

How can I get the primaryStage from my Main class into my controller?

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        // Select main layout file
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml"));

        // Add custom stylesheet URL
        root.getStylesheets().add(getClass().getResource("/css/stylesheet.css").toExternalForm());

        // Set Scene window params
        primaryStage.setTitle("React");
        primaryStage.setScene(new Scene(root, 1150, 600));
        primaryStage.setResizable(false);

        // Set task bar primary icon
        primaryStage.getIcons().add(new javafx.scene.image.Image("/images/react-app-icon.png"));

        // Show Scene
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • 1
    If the you want to get the `Stage` where the `TableView` is displayed, then `tableViewPriority.getScene().getWindow()` returns it. Otherwise you have to store the `Stage` in a member variable of the controller, and when you load the FXML file, set this member to the stage. For this, this answer surely helps: http://stackoverflow.com/questions/43582599/passing-data-to-the-controller-javafx – DVarga May 19 '17 at 11:45
  • I want precisely the ``Stage primaryStage`` parameter in the ``start()`` method of my ``Main.class``. – Martin Erlic May 19 '17 at 12:00
  • 2
    OK, so read the linked questions. – James_D May 19 '17 at 12:03

0 Answers0