简体   繁体   English

JavaFX:FileChooser正在闪烁

[英]JavaFX: FileChooser is blinking

It first appears in the left upper corner of the screen and then shows in the middle of the screen. 它首先出现在屏幕的左上角,然后显示在屏幕中间。

It is code: 这是代码:

 private static File fileChooserDialog(  final String initialDirectory, final String initialFileName, final boolean open,
                                        final String filterString, final String... extensions) {

    FileChooser fileChooser = new FileChooser();

    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(filterString, extensions);
    fileChooser.getExtensionFilters().add(extFilter);

    Stage stage = new Stage();

    File resultFile;

    if(open) {
        resultFile = fileChooser.showOpenDialog(stage);
    } else {
        resultFile = fileChooser.showSaveDialog(stage);
    }

    if(resultFile != null) {
        lastSelectedFilePath = resultFile.getParent();
    }

    return resultFile;
}

You should not create a new Stage every time you want to show a FileChooser . 每次要显示FileChooser时都不应创建新的Stage Remove this line: 删除此行:

Stage stage = new Stage();

And use your application's Window as an owner for the FileChooser . 并使用您的应用程序的Window作为FileChooser的所有者。 For example, if you are trying to show this dialog when the user clicks a button, you can get the Window like this: 例如,如果您在用户单击按钮时尝试显示此对话框,则可以像这样获取Window

Button button = new Button("Browse");
button.setOnAction(event -> {
    Window window = button.getScene().getWindow();
    fileChooser.showOpenDialog(window);
    event.consume();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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