Friday, August 23, 2013

JavaFX example: Modality

javafx.stage.Modality defines the possible modality types for a Stage.


package javafx_modality;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Modality extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        /*
         * initModality() much be called prior to making the stage visible. 
         * The modality is one of: Modality.NONE, Modality.WINDOW_MODAL, 
         * or Modality.APPLICATION_MODAL.
         */

        //NONE
        Button btnNONE = new Button();
        btnNONE.setText("Open NONE dialog");
        btnNONE.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage dialogNONE = new Stage();
                dialogNONE.initModality(Modality.NONE);

                Scene sceneNONE = new Scene(VBoxBuilder.create()
                        .children(
                            new Text("NONE dialog"), 
                            new Text("Defines a top-level window that is not "
                            + "modal and does not block any other window."))
                        .alignment(Pos.CENTER)
                        .padding(new Insets(10))
                        .build());

                dialogNONE.setTitle("NONE dialog");
                dialogNONE.setScene(sceneNONE);
                dialogNONE.show();
            }
        });
        
        //APPLICATION_MODAL
        Button btnAPPLICATION_MODAL = new Button();
        btnAPPLICATION_MODAL.setText("Open APPLICATION_MODAL dialog");
        btnAPPLICATION_MODAL.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage dialogAPPLICATION_MODAL = new Stage();
                dialogAPPLICATION_MODAL.initModality(Modality.APPLICATION_MODAL);

                Scene sceneAPPLICATION_MODAL = new Scene(VBoxBuilder.create()
                        .children(
                            new Text("APPLICATION_MODAL dialog"), 
                            new Text("Defines a modal window that blocks events "
                            + "from being delivered to any other application window."))
                        .alignment(Pos.CENTER)
                        .padding(new Insets(10))
                        .build());

                dialogAPPLICATION_MODAL.setTitle("APPLICATION_MODAL dialog");
                dialogAPPLICATION_MODAL.setScene(sceneAPPLICATION_MODAL);
                dialogAPPLICATION_MODAL.show();
            }
        });
        
        //WINDOW_MODAL
        Button btnWINDOW_MODAL = new Button();
        btnWINDOW_MODAL.setText("Open WINDOW_MODAL dialog");
        btnWINDOW_MODAL.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage dialogWINDOW_MODAL = new Stage();
                dialogWINDOW_MODAL.initModality(Modality.WINDOW_MODAL);

                Scene sceneWINDOW_MODAL = new Scene(VBoxBuilder.create()
                        .children(
                            new Text("WINDOW_MODAL dialog"), 
                            new Text("Defines a modal window that block events "
                            + "from being delivered to its entire owner window "
                            + "hierarchy."))
                        .alignment(Pos.CENTER)
                        .padding(new Insets(10))
                        .build());

                dialogWINDOW_MODAL.setTitle("WINDOW_MODAL dialog");
                dialogWINDOW_MODAL.setScene(sceneWINDOW_MODAL);
                dialogWINDOW_MODAL.show();
            }
        });
        
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        vBox.setSpacing(10);
        vBox.getChildren().addAll(
                btnNONE, 
                btnAPPLICATION_MODAL, 
                btnWINDOW_MODAL);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("java-buddy");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


2 comments:

  1. Hello, do you know if there is any way to make a dialog modal for a pane? Actually, what I need to do is to make it modal for just one tab, but get access to the others. Thank you!

    ReplyDelete