[英]How do I make JFileChooser open in the current directory the user is in?
我不想指定目录。 我只希望它自动“知道”并在用户正在使用的目录中打开。我该怎么做?
基本上,你不能。 您需要告诉它。
当使用null
currentDirectory
(例如默认构造函数)构造时,它将使用FileSystemView#getDefaultDirectory
。
您可以为每个基本任务创建一个JFileChooser
实例(一个用于保存,一个用于打开),并简单地维护该实例,它将“记住”它正在使用的最后一个目录,您仍然需要为其播种。虽然有一个起始目录
另一个选择是构造某种库调用,该库调用可以基于某个唯一键加载并保存用户使用的最后一个目录。 这意味着您可以简单地做类似...
File toFile = MyAwesomeLibrary.getSaveFile(APPLICATION_DOCUMENT_SAVE_KEY);
它将加载提供的密钥的最后一个已知目录,并显示使用该值配置的JFileChooser
,并且如果用户取消了操作,则能够返回所选的File
或null
……例如。
此示例在首次显示时默认为user.dir
。 然后,它保留选择器的实例以自动跟踪最后的加载或保存位置,如MadProgrammer所建议的那样。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
public class ChooserInCurrentDir {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
JFileChooser fileChooser;
private JTextArea output = new JTextArea(10, 40);
ChooserInCurrentDir() {
initComponents();
}
public final void initComponents() {
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
String userDirLocation = System.getProperty("user.dir");
File userDir = new File(userDirLocation);
// default to user directory
fileChooser = new JFileChooser(userDir);
Action open = new AbstractAction("Open") {
@Override
public void actionPerformed(ActionEvent e) {
int result = fileChooser.showOpenDialog(gui);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File f = fileChooser.getSelectedFile();
FileReader fr = new FileReader(f);
output.read(fr, f);
fr.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
};
Action save = new AbstractAction("Save") {
@Override
public void actionPerformed(ActionEvent e) {
int result = fileChooser.showSaveDialog(gui);
throw new UnsupportedOperationException("Not supported yet.");
}
};
JToolBar tb = new JToolBar();
gui.add(tb, BorderLayout.PAGE_START);
tb.add(open);
tb.add(save);
output.setWrapStyleWord(true);
output.setLineWrap(true);
gui.add(new JScrollPane(
output,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
}
public final JComponent getGui() {
return gui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ChooserInCurrentDir cicd = new ChooserInCurrentDir();
JFrame f = new JFrame("Chooser In Current Dir");
f.add(cicd.getGui());
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
如果要设置目录,它们位于文件选择器的上一个打开位置,则需要设置当前目录。
//This will set the directory to the directory they previously chose a file from.
fileChooser.setCurrentDirectory(fileChooser.getCurrentDirectory());
我不确定这是否是您要查找的内容,但是当他们选择一个文件然后去选择另一个文件时,它将保留上一个选择中的目录。
如果希望您的应用程序查找用户的工作目录,则可以尝试以下操作:
String curDir = System.getProperty("user.dir");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.