How to apply a "Blur" effect when a Stagemodal overlaps an already opened Stage in Javafx

Asked

Viewed 101 times

0

Well my doubt is the following, I would like to apply an effect in which, when I open a new window, the window that was superimposed is blurred I tried to use

public Stage stageModal(String controller, String titulo){
    try {
        AnchorPane root = new AnchorPane();
        root = FXMLLoader.load(getClass().getResource("/com/ProcessosJuridicos/view/"+controller+".fxml"));
        root.setStyle("-fx-background-color: black");
        Scene scene = new Scene(root);
        final Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle(titulo);
        GaussianBlur blur = new GaussianBlur(55);
        ColorAdjust adj = new ColorAdjust(0, -0.9, -0.5, 0);
        adj.setInput(blur);
        root.setEffect(adj);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
        return stage;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

the way I tried to do it applies the "effect" on the window in which it was opened

1 answer

0

Try this way, because I believe you should take the open Stage Owner as a modal and apply the effect;

GaussianBlur blur = new GaussianBlur(55);
final Stage stage = new Stage();
stage.getOwner().getScene().getRoot().setEffect(blur);

To remove the effect of the parent window when the modal is closed:

stage.setOnCloseRequest(event ->
   stage.getOwner().getScene().getRoot().setEffect(null)
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.