1

I have a window with a button. Clicking this button opens a modal window. Now, I want to close this second window by clicking a button, but I can't figure out how.

public class StartMenu extends Application {    
@Override
public void start(Stage primaryStage) {
final Button b = new Button("Go");
b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {               
           Stage stage = new Stage();
           stage.initModality(Modality.APPLICATION_MODAL);
           AnotherWindow aw = new AnotherWindow ();               
           aw.start(stage);                
        }
    });
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);        
primaryStage.show();
}}

   

public class AnotherWindow extends Application {    
@Override
public void start(Stage primaryStage) {
final Button b = new Button("Back");
b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {               
           //Code to close window               
        }
    });
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);        
primaryStage.show();
}}
aMist
  • 46
  • 3

2 Answers2

1

I found the following post by Krzysztof Sz. that helped me find the solution.

public class AnotherWindow extends Application {    
    @Override
    public void start(Stage primaryStage) {
    final Button b = new Button("Back");
    b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {               
           ((Button)t.getTarget()).getScene().getWindow().hide();              
        }
    });
    ((Group) scene.getRoot()).getChildren().add(b);
    primaryStage.setScene(scene);        
    primaryStage.show();
    }}

 

It is the following piece of code that let's me close the current (modal) window when the button is clicked:

    ((Button)t.getTarget()).getScene().getWindow().hide();
Community
  • 1
  • 1
aMist
  • 46
  • 3
0

You want to close a modal window from a click on a different window? If a modal window is visible, how will you get back to the other window?

You might want to use one window: when a button is clicked, hide all the controls in that window and make visible the information you wanted to have in your modal window, along with a button to click. When that button is clicked, reset the window to its' original state.

This just becomes an exercise in showing/hiding controls in a container.

nicomp
  • 4,344
  • 4
  • 27
  • 60