【问题标题】:JavaFX - Why is my FileChooser giving me access to the origin Stage?JavaFX - 为什么我的 FileChooser 允许我访问原始阶段?
【发布时间】:2015-06-13 16:45:50
【问题描述】:

当我单击按钮时,会打开 FileChooser。但是,例如,我可以在 FileChooser 仍然打开时关闭 Original Stage,或者我仍然可以单击并切换实际窗口。检查下面的代码

我的问题是:
1- 当我关闭主窗口时,如何使 FileChooser 关闭?
2- 打开 FileChooser 时如何使主窗口不可点击?

package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;

public class Main extends Application {

    @Override public void start(Stage stage) {

        stage.setTitle("Main Stage");
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();
        Button button = new Button();
        AnchorPane ap = new AnchorPane();
        Scene scene = new Scene(ap);
        ap.getChildren().addAll(button);
        stage.setScene(scene);

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                FileChooser fileChooser = new FileChooser();
                Stage stage2=new Stage();
                stage2.initOwner(stage);
                stage2.initModality(Modality.WINDOW_MODAL);
                fileChooser.showOpenDialog(stage2);  
           }
       });  
    }

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

【问题讨论】:

    标签: events button javafx javafx-8 filechooser


    【解决方案1】:

    根据JavaDocs

    如果设置了文件对话框的所有者窗口,则输入到所有窗口 对话框的所有者链中的文件对话框正在被阻止 显示。

    但是,您将所有者窗口设置为不在屏幕上的窗口,因此我认为在这种情况下不存在“所有者链”,并且文件选择器实际上不是模态的。

    为什么不这样做

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                FileChooser fileChooser = new FileChooser();
                fileChooser.showOpenDialog(stage); 
           }
       });
    

    让文件选择器的所有者窗口成为实际窗口?

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 2015-04-07
      • 2017-02-21
      • 2020-11-24
      • 1970-01-01
      • 2019-10-16
      • 2014-07-12
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多