JavaFX Dialog Box

public void exitGame() { //pop up dialogue
    Platform.exit();
}

I tried a lot of different things that I saw on the Internet, but I can not get anything to work. All I need is a simple dialog box that appears when you click the "Exit" button on the menu and ask, "Are you sure you want to exit?". and offers the job "Yes, I'm sure." and "No, cancel."

+4
source share
2 answers

Here is an example JavaFX dialog. JavaFx 2.x does not have an api dialog. Thus, you created your own stage and created components in it.

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

/**
 * @author JayaPrasad
 * 
 */
public class SampleDialog extends Application {

    /*
     * (non-Javadoc)
     * 
     * @see javafx.application.Application#start(javafx.stage.Stage)
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("JavaFx Dialog");
        Button btn = new Button();
        btn.setText("Click me to display popup dialog");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                final Stage dialogStage = new Stage();
                dialogStage.initModality(Modality.WINDOW_MODAL);

                Label exitLabel = new Label("Are you sure you want to exit?");
                exitLabel.setAlignment(Pos.BASELINE_CENTER);

                Button yesBtn = new Button("Yes");
                yesBtn.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent arg0) {
                        dialogStage.close();

                    }
                });
                Button noBtn = new Button("No");

                noBtn.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent arg0) {
                        dialogStage.close();

                    }
                });

                HBox hBox = new HBox();
                hBox.setAlignment(Pos.BASELINE_CENTER);
                hBox.setSpacing(40.0);
                hBox.getChildren().addAll(yesBtn, noBtn);

                VBox vBox = new VBox();
                vBox.setSpacing(40.0);
                vBox.getChildren().addAll(exitLabel, hBox);

                dialogStage.setScene(new Scene(vBox));
                dialogStage.show();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
+6
source

Lambdas are also nice:

  yesBtn.setOnAction (e -> dialogStage.close ());
  noBtn.setOnAction (e -> dialogStage.close ());

Also APPLICATION_MODAL, probably you want:

  dialogStage.initModality (Modality.APPLICATION_MODAL);
+2
source

Source: https://habr.com/ru/post/1533931/


All Articles