将程序的目录设置为JavaFX FileChooser

时间:2017-05-16 13:47:24

标签: java javafx path filechooser

我正在使用JavaFX。我想从程序目录开始FileChooser,因此初始存储库应该是程序的存储库。

这是我的FileChooser声明:

FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().addAll(
new ExtensionFilter("Text Files", "*.txt"),
new ExtensionFilter("All Files", "*.*"));
chooser.setTitle("Choisir un fichier");
file = chooser.showOpenDialog(new Stage());

我该怎么做?

1 个答案:

答案 0 :(得分:2)

目前的目录是“。”。以下是如何做到这一点:

FileChooser chooser = new FileChooser();
String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();
chooser.setInitialDirectory(new File(currentPath));
chooser.showOpenDialog(new Stage());

编辑:您应该传递给FileChooser的舞台或javafx节点是您想要成为其父节点的节点。