[英]Block owner window Java FX
我想在 JavaFX 中阻止彈出窗口的所有者窗口。
我像這樣初始化我的彈出窗口:
popUp = new Popup();
popUp.getContent().add(content);
popUp.show(pane.getScene().getWindow());
有了這個,我仍然可以在第一個窗口(窗格窗口)中工作。 我想禁用此操作,並且我希望用戶只在彈出窗口中工作。
這個怎么做 ?
謝謝。
在顯示舞台之前,根據需要調用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();
謝謝,最佳解決方案:帶有 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);
}
我正在使用 JavaFX11。 無論這個和這個作品有小的變化。 這是我的工作示例。 我正在使用rgielen 的 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();
}
}
你只需要從 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.