java 如何从 JavaFX FileChooser 获取文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35450990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file path from JavaFX FileChooser?
提问by Hans
I have a simple JavaFX window with a TextField for users to enter a file path and a separate browse link.
我有一个带有 TextField 的简单 JavaFX 窗口,供用户输入文件路径和单独的浏览链接。
I'd like to ask how to extract the full file path of the selected file from JavaFX FileChooser (so I can set the path in the TextField)?
我想问一下如何从JavaFX FileChooser中提取所选文件的完整文件路径(以便我可以在TextField中设置路径)?
I understand what I'm trying to achieve can be done simply with Swing JFileChooser with something like:
我明白我想要实现的目标可以简单地使用 Swing JFileChooser 完成,例如:
JFileChooser chooser = new JFileChooser();
String someString = chooser.getSelectedFile().toString();
But since my application is in JavaFX I want it to have a consistent look and not a mix with Swing.
但由于我的应用程序是在 JavaFX 中,我希望它具有一致的外观,而不是与 Swing 混合。
I've looked through the documentation, there doesn't seem to be a method for this https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html
我查看了文档,似乎没有针对此https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html的方法
Thanks in advance.
提前致谢。
回答by Itai
Use showOpenDialog
or showSaveDialog
(depending on whether you want to open an existing file or save a new one). Both return a File
object.
使用showOpenDialog
或showSaveDialog
(取决于您是要打开现有文件还是保存新文件)。两者都返回一个File
对象。
回答by Roland
Here's another documentation. What you get in return from using showOpenDialogis a Fileobject.
这是另一个文档。使用showOpenDialog得到的回报是一个File对象。
public File showOpenDialog(Window ownerWindow)
Shows a new file open dialog. The method doesn't return until the displayed open dialog is dismissed. The return value specifies the file chosen by the user or null if no selection has been made. If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.
公共文件 showOpenDialog(Window ownerWindow)
显示一个新文件打开对话框。在显示的打开对话框被关闭之前,该方法不会返回。返回值指定用户选择的文件,如果未进行选择,则返回 null。如果设置了文件对话框的所有者窗口,则在显示文件对话框时会阻止输入到对话框所有者链中的所有窗口。
A file object has various methods like e. g. getAbsolutePath.
文件对象具有各种方法,例如getAbsolutePath。
回答by Marinel P
In the controller class where you have the TextField you can create a method like so:
在您拥有 TextField 的控制器类中,您可以创建一个如下所示的方法:
public void getTheUserFilePath() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Upload File Path");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("ALL FILES", "*.*"),
new FileChooser.ExtensionFilter("ZIP", "*.zip"),
new FileChooser.ExtensionFilter("PDF", "*.pdf"),
new FileChooser.ExtensionFilter("TEXT", "*.txt"),
new FileChooser.ExtensionFilter("IMAGE FILES", "*.jpg", "*.png", "*.gif")
);
File file = fileChooser.showOpenDialog(dialogPane.getScene().getWindow());
if (file != null) {
// pickUpPathField it's your TextField fx:id
pickUpPathField.setText(file.getPath());
} else {
System.out.println("error"); // or something else
}
}