我想在JavaFX中阻止弹出窗口的所有者窗口。
我像这样初始化我的弹出窗口:
popUp = new Popup();
popUp.getContent().add(content);
popUp.show(pane.getScene().getWindow());
有了这个,我仍然可以在第一个窗口(窗格窗口)中工作。我想禁用此操作,我希望用户只需在弹出窗口中工作。
怎么做?
感谢。
答案 0 :(得分:17)
在显示该阶段之前,请根据需要将stage.initModality调用为APPLICATION_MODAL或WINDOW_MODAL。同时调用stage.initOwner到新舞台的父窗口,以便它适当地阻止WINDOW_MODAL
案例。
Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(pane.getScene().getWindow());
stage.setScene(new Scene(content));
stage.show();
答案 1 :(得分:3)
谢谢,最佳解决方案: FXML加载文件的示例:
@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);
}
答案 2 :(得分:0)
我使用的是 JavaFX11。 this 和 this 只需稍作改动即可工作。这是我的工作示例。我正在使用 rgielen's javafx-weaver
主要内容:
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()
);
}
}
对话:
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();
}
}
答案 3 :(得分:0)
您只需要从 javafx.stage.Modality 添加 Modality 资源;
然后添加一行 stage.initModality(Modality.APPLICATION_MODAL);
这里有一个例子
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);