如何在java swing库中浏览文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/282779/
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 browse for a file in java swing library?
提问by Tomek
I was wondering if there was some kind of J tool in the java swing library that opens up a file browser window and allows a user to choose a file. Then the ouput of the file would be the absolute path of the chosen file.
我想知道 java swing 库中是否有某种 J 工具可以打开文件浏览器窗口并允许用户选择文件。然后文件的输出将是所选文件的绝对路径。
Thanks in advance,
提前致谢,
采纳答案by CMS
You can use the JFileChooserclass, check this example.
您可以使用JFileChooser类,请查看此示例。
回答by Tomek
I ended up using this quick piece of code that did exactly what I needed:
我最终使用了这段快速的代码,它完全符合我的需要:
final JFileChooser fc = new JFileChooser();
fc.showOpenDialog(this);
try {
// Open an input stream
Scanner reader = new Scanner(fc.getSelectedFile());
}
回答by iberck
The following example creates a file chooser and displays it as first an open-file dialog and then as a save-file dialog:
以下示例创建一个文件选择器,并首先将其显示为打开文件对话框,然后显示为保存文件对话框:
String filename = File.separator+"tmp";
JFileChooser fc = new JFileChooser(new File(filename));
// Show open dialog; this method does not return until the dialog is closed
fc.showOpenDialog(frame);
File selFile = fc.getSelectedFile();
// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(frame);
selFile = fc.getSelectedFile();
Here is a more elaborate example that creates two buttons that create and show file chooser dialogs.
这是一个更详细的示例,它创建了两个按钮,用于创建和显示文件选择器对话框。
// This action creates and shows a modal open-file dialog.
public class OpenFileAction extends AbstractAction {
JFrame frame;
JFileChooser chooser;
OpenFileAction(JFrame frame, JFileChooser chooser) {
super("Open...");
this.chooser = chooser;
this.frame = frame;
}
public void actionPerformed(ActionEvent evt) {
// Show dialog; this method does not return until dialog is closed
chooser.showOpenDialog(frame);
// Get the selected file
File file = chooser.getSelectedFile();
}
};
// This action creates and shows a modal save-file dialog.
public class SaveFileAction extends AbstractAction {
JFileChooser chooser;
JFrame frame;
SaveFileAction(JFrame frame, JFileChooser chooser) {
super("Save As...");
this.chooser = chooser;
this.frame = frame;
}
public void actionPerformed(ActionEvent evt) {
// Show dialog; this method does not return until dialog is closed
chooser.showSaveDialog(frame);
// Get the selected file
File file = chooser.getSelectedFile();
}
};
回答by Tom Hawtin - tackline
In WebStart and the new 6u10 PlugIn you can use the FileOpenService, even without security permissions. For obvious reasons, you only get the file contents, not the file path.
在 WebStart 和新的 6u10 插件中,即使没有安全权限,您也可以使用 FileOpenService。出于显而易见的原因,您只能获取文件内容,而不是文件路径。