0

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.
  • Try setting the "addScene" to a new Stage rather than to the current primaryStage. Stage newStage = new Stage(); newStage.setScene(addScene); – Sai Dandem Nov 07 '19 at 04:30

1 Answers1

0

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();
    }
});
Sai Dandem
  • 8,229
  • 11
  • 26
  • The button is not showing "Add" and clicking it does not result in any action. – Mo Hassanpour Nov 07 '19 at 05:32
  • The button was not showing "Add" and clicking it did not result in any action.Then I realized I trusted the answer so the problem must be elsewhere. I looked at the buttons on my pane to make sure the unlabeled button was really the save/add button. I then realized I was setting the add function to the save button, but had an add button on the pane. ... Its always the simple stuff that gets you. We assume it has to be something we are doing wrong when its really more likely careless error. Thank you @sai – Mo Hassanpour Nov 07 '19 at 05:43