Catersa
Catersa

Reputation: 125

JavaFX Stage Modality

I am trying to make a Modal window, opened through a Stage. This is the code:

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

But with no actual success, as I am able though, to click the underlying windows. Is there anything that I am doing wrong?

EDIT: I checked earlier the documentation at Oracle's site, but could find a solution, as the displayed window's behavior wasn't any close to what its expected from a Modal Window. The JDK I am using on this project is 1.8.0_40

EDIT 2: The code within the handle, as requested by 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);
    }
}

Thank you for your time and help.

Upvotes: 0

Views: 14418

Answers (3)

hasindu-s
hasindu-s

Reputation: 1

Replace stage.initModality(Modality.WINDOW_MODAL);
with stage.initModality(Modality.APPLICATION_MODAL);

You also have to replace stage.show();
with stage.showAndWait();

Upvotes: 0

AlmasB
AlmasB

Reputation: 3407

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

Upvotes: 4

ItachiUchiha
ItachiUchiha

Reputation: 36722

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.

Upvotes: 4

Related Questions