Search code examples
javajavafxscenestage

How to create button opening another window in JavaFx w/o GUI builder, FXML, swing, or awt?


I need to create a button to open a window in java fx. Not swing. No fxml.

A. Can you have more than one stage? I think not, but I saw a site saying otherwise.

B. do I have to make the button create a class that extends application, override start() ...

I've looked at the following to no avail. I've seen a lot, I just dont understand what's there or how to implement it. How do I modify my code to make it work?

https://o7planning.org/en/11533/opening-a-new-window-in-javafx
https://coderanch.com/t/663804/java/call-window-click-button-current
https://o7planning.org/en/11533/opening-a-new-window-in-javafx

Button to push new customers onto queue. Opens a new popu window (stage) to enter customer information.

        btSave.setText("Add");
        btSave.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {                

                //SCENE 3 OF 3: ADD SCENE, Pane & Nodes
                GridPane addPane = new GridPane();

                //add nodes
                addPane.add(new Label("Customer Name"), 1,1);
                addPane.add(tfName, 1,2);
                addPane.add(new Label("Size of Party"), 1,3);
                addPane.add(tfParty, 1,4);
                addPane.add(new Label("Customer Phone Number"), 1,5);
                addPane.add(tfPhone, 1,6);
                addPane.add(btSave, 1, 7);

                Scene addScene = new Scene(addPane, 300, 250);
                primaryStage.setTitle("Add Customer to Queue");
                primaryStage.setScene(addScene);
                primaryStage.show();

    //                Customer cust = new Customer();
    //                cust.name = tfName.getText();
    //                cust.party = Integer.parseInt(tfParty.getText());
    //                cust.phone = Integer.parseInt(tfPhone.getText()); 
    //                qLine.offer(cust);
                }
            });

I would like the button to show "Add" and would like it to open a new window when clicked.

Solution

  • If you are asking about opening a new Stage, the below code may help you.

    Button btSave = new Button();
    btSave.setText("Add");
    btSave.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {                
    
            //SCENE 3 OF 3: ADD SCENE, Pane & Nodes
            GridPane addPane = new GridPane();
    
            //add nodes
            addPane.add(new Label("Customer Name"), 1,1);
            addPane.add(tfName, 1,2);
            addPane.add(new Label("Size of Party"), 1,3);
            addPane.add(tfParty, 1,4);
            addPane.add(new Label("Customer Phone Number"), 1,5);
            addPane.add(tfPhone, 1,6);
            addPane.add(btSave, 1, 7);
    
            Stage addStage = new Stage();
            Scene addScene = new Scene(addPane, 300, 250);
            addStage.setTitle("Add Customer to Queue");
            addStage.setScene(addScene);
            addStage.show();
        }
    });