user7819089
user7819089

Reputation:

How to close popup window by button in javafx

I'm trying to close popup window by button, but I don't have any idea. When I used Java Swing, I remember the function was automatic...

So.. What should I do?

And I also want to make more space between button and text. If you have any idea, please help me.

                    Stage dialogStage = new Stage();
                    dialogStage.initModality(Modality.WINDOW_MODAL);

                    Button button = new Button();
                    button.setOnKeyPressed(new EventHandler<KeyEvent>() {
                        @Override
                        public void handle(KeyEvent event) {
                            if (event.getCode() == KeyCode.ENTER) {


                            }
                        }
                    });

                    VBox vbox = new VBox(new Text("Wrong ID & PW!!"), new Button("Ok."));
                    vbox.setAlignment(Pos.CENTER);
                    vbox.setPadding(new Insets(15));

                    dialogStage.setScene(new Scene(vbox));
                    dialogStage.show();

Upvotes: 0

Views: 5747

Answers (1)

Courage
Courage

Reputation: 792

Call dialogStage.close()

 button.setOnKeyPressed(new EventHandler<KeyEvent>() {
                    @Override
                    public void handle(KeyEvent event) {
                        if (event.getCode() == KeyCode.ENTER) {
                            dialogStage.close();
                        }
                    }
                });

Also you are adding a new Button("Ok.") to the VBox which is wrong, add the button which you created before

As for the space between the button and text, this should work

 VBox.setMargin(text, new Insets(20));
 VBox.setMargin(button, new Insets(20));

Upvotes: 1

Related Questions