0

I am trying to open new stage when the button 'Add Task' is being clicked. Currently I have the following code for the main Class ( I didn't include the imports )

public class Main extends Application
{

    Stage window;

    private Stage primaryStage;
    private BorderPane mainLayout;


    @Override
    public void start(Stage primaryStage) throws Exception 
    {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Test");
        showMainView(); 

    }

    private void showMainView() throws IOException
    {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Main.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public void showAddStage() throws IOException
    {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/AddTask.fxml"));
        Parent addTask = loader.load();

        Stage addTaskStage = new Stage();
        addTaskStage.setTitle("Add Task");
        addTaskStage.initModality(Modality.WINDOW_MODAL);
        addTaskStage.initOwner(primaryStage);
        Scene addTaskscene = new Scene(addTask);
        addTaskStage.setScene(addTaskscene);
        addTaskStage.showAndWait();
    }


    public static void main(String[] args)
    {
        launch(args);
    }   

}

and I have the following for my Main.fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AddTaskController">
   <top>
      <Button mnemonicParsing="false" onAction="#addTask" prefHeight="25.0" prefWidth="615.0" text="ADD TASK" BorderPane.alignment="CENTER" />
   </top>
</BorderPane>

I have this for the AddTask.fxml ( the window which supposed to be opened when the 'Add Task' button is pressed )

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane prefHeight="500.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <bottom>
      <HBox prefHeight="40.0" prefWidth="200.0" spacing="10.0" BorderPane.alignment="CENTER">
         <children>
            <Button mnemonicParsing="false" prefWidth="80.0" text="Add" />
            <Button mnemonicParsing="false" prefWidth="80.0" text="Cancel" />
         </children>
      </HBox>
   </bottom>
</BorderPane>

and this for the controller

package controller;

import java.io.IOException;
import Driver.Main;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

public class AddTaskController 
{
    private Main main;

    @FXML
    private void addTask() throws IOException
    {
        System.out.println("Hello World")
        main.showAddStage();
    }
}

This line System.out.println("Hello World") is only for test. So when I click on the button Hello world is being printed which means that my button works fine. But, the problem is that the new window AddTask.fxml doesn't get opened when the button is pressed.

Please if you have any question or request for code, comment it out. This question is different from the one that is being mentioned. I don't want to pass parameters but rather just open a new window when a button is being clicked.

Thanks

user8202693
  • 159
  • 1
  • 3
  • 13
  • @fabian this is not a duplicated. I don't want to pass parameters. I want to open new window when a button is pressed. – user8202693 Nov 12 '17 at 17:23
  • 1
    Then perhaps you can post where you are initializing `main`, before referencing it in `addTask()`. The code you posted would throw an exception. – James_D Nov 12 '17 at 17:43

0 Answers0