File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JFileChooser, FileDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFileChooser, FileDialog" Watch "JFileChooser, FileDialog" New topic

JFileChooser, FileDialog

hema narayan

Joined: Dec 28, 2001
Posts: 6
i want that when i select a file from a specific location using JFileChooser then it must save the path of that file upto some location, so that next when i open JFileChooser Dialog to open some other file then i do not have to specify it the path of that file from the root, since they being in the same directory.
Same i want in the case of FileDialog also.
i hope that the problem is clear.
plz reply me soob
from hema
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
For JFileChooser
JFileChooser chooser = new JFileChooser();
//set to the current directory
JFileChooser chooser =
new JFileChooser(System.getProperty("user.dir"));
Once the user selects a file, do the following

getParent() returns you a String while getParentFile() returns you a File object
For FileDialog
do following after the user selects a file
String dir = fd.getDirectory();
if (dir != null)
[ January 24, 2002: Message edited by: kawaii desu ]
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
Thanks Kawaii that's detailed and good information.
hema narayan

Joined: Dec 28, 2001
Posts: 6
Thanks Kawaii
for your detailed and
well explained answer to my problem
suheel hussain

Joined: Nov 01, 2001
Posts: 16
Another easy way is to create JFileChooser at the start of application and keep this chooser until end of application.
public class blahblah {
private JFileChooser chooser = new JFileChooser;
// local public and private functions
} // end of application
since chooser has a class scope, it retains last selected directory/file information.
I agree. Here's the link:
subject: JFileChooser, FileDialog
jQuery in Action, 3rd edition