Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Modality in JavaFX

darrylburkeJun 29 2011 — edited Jul 1 2011
In Swing (and in other languages I've coded in) showing a modal dialog holds up the execution of subsequent code until the dialog is closed. It appears that JavaFX follows a different concept of 'modal' -- is this by design? Demo code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class ModalityTest extends Application{
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(final Stage primaryStage) throws Exception {
    final Button button = new Button("Launch modal dialog");
    button.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
        final Stage stage = new Stage();
        stage.setOnHiding(new EventHandler<WindowEvent>() {

          @Override
          public void handle(WindowEvent event) {
            button.setText("Launch modal dialog");
          }
        });
        Button button1 = new Button("Close");
        button1.setOnAction(new EventHandler<ActionEvent>() {

          @Override
          public void handle(ActionEvent event) {
            stage.hide();
          }
        });
        Scene scene1 = new Scene(button1);
        stage.initModality(Modality.APPLICATION_MODAL);
        // also tried WINDOW_MODAL
        stage.initOwner(primaryStage);
        stage.setScene(scene1);
        stage.setVisible(true);
        // Note that the next line is executed immediately, before the dialog is hidden
        button.setText("Dialog shown");
      }
    });
    VBox vBox = new VBox();
    vBox.getChildren().add(button);
    Scene scene = new Scene(vBox, 200, 200);
    primaryStage.setScene(scene);
    primaryStage.setVisible(true);
  }
}
db
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 29 2011
Added on Jun 29 2011
7 comments
2,410 views