0

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;
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36

1 Answers1

0

You should not create a new Stage every time you want to show a FileChooser. Remove this line:

Stage stage = new Stage();

And use your application's Window as an owner for the FileChooser. For example, if you are trying to show this dialog when the user clicks a button, you can get the Window like this:

Button button = new Button("Browse");
button.setOnAction(event -> {
    Window window = button.getScene().getWindow();
    fileChooser.showOpenDialog(window);
    event.consume();
});
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36