jfilechooser - set directory to a path in a file

我与影子孤独终老i 提交于 2019-12-03 23:31:02

Try to pass the current directory directly in the constructor:

filechooser = new JFileChooser(theDirectory);

If you consult the API, using the default constructor (i.e. new JFileChooser()):

Constructs a JFileChooser pointing to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.

This would seem to account for always opening to My Documents, but this isn't your problem. In fact, your problem lies with setting the current directory (i.e. setCurrentDirectory(theDirectory)):

Sets the current directory. Passing in null sets the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix. If the file passed in as currentDirectory is not a directory, the parent of the file will be used as the currentDirectory. If the parent is not traversable, then it will walk up the parent tree until it finds a traversable directory, or hits the root of the file system.

That being said, I'd pay attention to the highlighted text since it appears that you're setting a file as the current directory and not a directory.

For select the last directory that you open :

chooser.setCurrentDirectory(lastDirectory);

int r = chooser.showOpenDialog(new JPanel());

if (r == JFileChooser.APPROVE_OPTION) {
   fileName = chooser.getSelectedFile().getPath();
   lastDirectory = chooser.getSelectedFile();
}

JFileChooser Chooser = new JFileChooser("F:");

pavan pandey

In your main class declare

public static String dirpath=".";

private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {    
 JFileChooser jfc = new JFileChooser(dirpath);
 dirpath =jfc.getSelectedFile().getAbsolutePath().toString();
}

if you want to change the directory theb use System.getProperty method

String s=System.getProperty("user.dir");  // changes directory from documents to the user current Directory;

JFileChooser jfc=new JFileChooser(s);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!