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