我正在使用 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());
我怎样才能做到这一点?
最佳答案
当前目录是“.”。以下是您可以执行此操作的方法:
FileChooser chooser = new FileChooser();
String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();
chooser.setInitialDirectory(new File(currentPath));
chooser.showOpenDialog(new Stage());
编辑:您应该传递给 FileChooser 的 Stage 或 javafx 节点是您希望成为其父级的节点。
关于java - 将程序目录设置为JavaFX FileChooser的初始目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003330/