简体   繁体   中英

Block owner window Java FX

I would like to block the owner window for a popup in JavaFX.

I initialize my popup like this:

popUp = new Popup();
popUp.getContent().add(content);
popUp.show(pane.getScene().getWindow());

With this, I can still work in the first window (pane window). I would like to disable this action and I would like the user just works in the popup.

How to do this ?

Thanks.

Use a Stage instead of a Popup .

Before showing the stage, invoke stage.initModality as either APPLICATION_MODAL or WINDOW_MODAL , as appropriate. Also invoke stage.initOwner to the parent window of your new stage so that it will appropriately block it for the WINDOW_MODAL case.

Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(pane.getScene().getWindow());
stage.setScene(new Scene(content));
stage.show();

Thanks, optimal solution: example with FXML load file:

@Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("DialogView.fxml"));
        primaryStage.initModality(Modality.APPLICATION_MODAL); // 1 Add one
        Scene scene = new Scene(root);        
        primaryStage.setScene(scene);
        primaryStage.initOwner(primaryStage.getScene().getWindow());// 2 Add two
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);

    }

I'm using JavaFX11. Both this and this works with a small change. Here is my working example. I'm using rgielen's javafx-weaver

Main:

package com.featuriz.controller;

import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import net.rgielen.fxweaver.core.FxControllerAndView;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.stereotype.Component;

@Component
@FxmlView("/com/featuriz/ui/Login.fxml")
public class LoginController {
    
    private final FxControllerAndView<InfoDialogController, AnchorPane> infoDialog;

    @FXML
    private Text featuriz;

    public LoginController(FxControllerAndView<InfoDialogController, AnchorPane> infoDialog) {
        this.infoDialog = infoDialog;
    }

    @FXML
    public void initialize() {
        featuriz.setOnMouseClicked(
                actionEvent -> this.infoDialog.getController().show()
        );
    }
}

Dialog:

package com.featuriz.controller;

import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.stereotype.Component;

@Component
@FxmlView("/com/featuriz/ui/InfoDialog.fxml")
public class InfoDialogController {

    private Stage stage;

    @FXML
    private Label lbl_info;

    @FXML
    private Button closeButton;

    @FXML
    private AnchorPane dialog_info;

    @FXML
    public void initialize() {
        Scene scene = new Scene(dialog_info);
        scene.setFill(Color.TRANSPARENT);
        this.stage = new Stage();
        this.stage.initModality(Modality.APPLICATION_MODAL);
        this.stage.initStyle(StageStyle.TRANSPARENT);
        this.stage.setScene(scene);
        this.stage.setAlwaysOnTop(true);

        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        lbl_info.setText("JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");

        closeButton.setOnAction(
                actionEvent -> this.stage.close()
        );
    }

    public void show() {
        this.stage.show();
    }

}

You just have to add the Modality resource from javafx.stage.Modality;

And then add the line stage.initModality(Modality.APPLICATION_MODAL);

Here you have an example

 Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource( -- pathInterface --)); root.getStylesheets().add(getClass().getResource( -- pathCss --).toExternalForm()); stage.initStyle(StageStyle.DECORATED.UNDECORATED); stage.initModality(Modality.APPLICATION_MODAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM