JavaFX阶段模态

时间:2015-06-25 09:53:55

标签: java javafx

我正在尝试创建一个Modal窗口,通过舞台打开。这是代码:

Stage stage = new Stage();                               
Parent root = fxmlLoader.load();
stage.setScene(new Scene(root));
stage.setTitle("Stuff");
stage.initModality(Modality.WINDOW_MODAL);
stage.show();

但是,虽然没有实际成功,但我能够点击基础窗口。 有什么我做错了吗?

修改 我之前检查过Oracle网站上的文档,但是可以找到一个解决方案,因为显示的窗口的行为与Modal Window的预期没有任何接近。 我在这个项目上使用的JDK是1.8.0_40

编辑2: 句柄中的代码,按照ItachiUchiha的要求

Boolean confirmacion = MessageUtil.ventanaConfirmacion(RecursosUtil.creaTextoProperties("confNoRealizar"));

if (confirmacion) {
    try {
        TbInspeccion inspeccionSeleccionada = (TbInspeccion) getTableRow().getItem();

        URL location = cargaURLFXML(OBSERVACIONESFXML);
        FXMLLoader fxmlLoader = cargaFXML(location);

        stage = new Stage();
        Parent root = fxmlLoader.load();
        stage.setScene(new Scene(root));
        stage.setTitle("Observaciones");

        ObservacionesController controller = (ObservacionesController) fxmlLoader.getController();
        controller.setStage(stage);
        controller.setInspeccion(inspeccionSeleccionada);
        controller.setProvider(provider);
        controller.setTablaPrincipal(tablaPrincipal);

        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

感谢您的时间和帮助。

3 个答案:

答案 0 :(得分:3)

Replace

stage.initModality(Modality.WINDOW_MODAL);

with

stage.initModality(Modality.APPLICATION_MODAL);

The former blocks as follows:

Defines a modal window that block events from being delivered to its entire owner window hierarchy.

Whereas the latter:

Defines a modal window that blocks events from being delivered to any other application window.

Source: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Modality.html

Depending on why you want to achieve the aforementioned behaviour you might want to use JavaFX 8 Dialog available from JDK 8 Update 40

答案 1 :(得分:1)

You need to add stage.initOwner() along with Modality.WINDOW_MODAL to that it identifies the owner :

stage.initOwner(primaryStage);
stage.initModality(Modality.WINDOW_MODAL);

Or, you can just use Modality.APPLICATION_MODAL without an owner.

答案 2 :(得分:0)

替换stage.initModality(Modality.WINDOW_MODAL);
stage.initModality(Modality.APPLICATION_MODAL);

您还必须替换 stage.show();
stage.showAndWait();