1
votes

I'm creating a JavaFX application which has a main Stage then calls other methods to create sub windows (also stages). This works fine. But the sub-window's close button does not work & i have to close the window using the "X" in the top right hand side. It seems that the event handler isn't being called at all.

Main snippet from the sub-window below:

Apologies for the formatting - doesn't seem to translate properly..

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.Font;
    import javafx.stage.Stage;
    import javafx.scene.layout.FlowPane;
    import javafx.scene.control.Button;
    import javafx.geometry.Pos;
    import javafx.scene.text.TextAlignment;
    import javafx.stage.Modality;

    /**
     * Provides a Treasury Report & displays balances until a Button is pressed
     *
     * @author (your name)
     * @version (a version number or a date)
     */
    public class TreasuryReport extends Stage
    {
        /**
         * Constructor for objects of class TreasuryReport
         */
        public void ShowTreasuryReport(int acBalance, int mthCosts, int swissAcc)
        {
            Stage treasuryStage = new Stage();
            treasuryStage.setTitle("Treasury Report");
            treasuryStage.initModality(Modality.WINDOW_MODAL);
            int absBalance = acBalance;
            Button closeButton = new Button ("Close Report"); 

There's other bits where I create a label which is displayed

    VBox layout1 = new VBox(20);     
    layout1.setAlignment(Pos.CENTER);
    layout1.getChildren().addAll(label1, closeButton);
    Scene TRScene = new Scene(layout1, 300, 250);
    treasuryStage.setScene(TRScene);
    treasuryStage.showAndWait();

    closeButton.setOnAction(new EventHandler<ActionEvent>() 
        {
            public void handle(ActionEvent e) {
                System.out.println("TR button pressed");
                treasuryStage.hide();
                treasuryStage.close();
            }
        });
}

I've tried lots of different options and nothing appears to work. It seems like the button does not even go to the event handler as the println statement is not generated. I've spent many fruitless hours reading through various options. All help gratefully received.

1
You should read this. It surely can help.Pagbo

1 Answers

2
votes

showAndWait(), as its name suggests, shows the stage and then waits (blocks execution) until the stage is closed. Consequently, you don't register the handler with the button until the stage has already closed.

Simply change the order of execution of the code:

public void ShowTreasuryReport(int acBalance, int mthCosts, int swissAcc) {
    Stage treasuryStage = new Stage();
    treasuryStage.setTitle("Treasury Report");
    treasuryStage.initModality(Modality.WINDOW_MODAL);
    int absBalance = acBalance;
    Button closeButton = new Button ("Close Report"); 

    closeButton.setOnAction(event ->  {
        System.out.println("TR button pressed");
        treasuryStage.hide();
    });

    VBox layout1 = new VBox(20);     
    layout1.setAlignment(Pos.CENTER);
    layout1.getChildren().addAll(label1, closeButton);
    Scene TRScene = new Scene(layout1, 300, 250);
    treasuryStage.setScene(TRScene);
    treasuryStage.showAndWait();


}