Results 1 to 4 of 4
- 12-05-2007, 11:47 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 12
- Rep Power
- 0
JFileChooser remember the location
I'm trying to get the JFileChooser to remember the location of the previous location opened, and then next time open there, but is doesn't seem to remember.
I have tried to do this using the following method:
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(jButton2);
if (theOutString != null){
fc.setCurrentDirectory(new File(theOutString)); }
if(returnVal == JFileChooser.APPROVE_OPTION) {
theOutString = fc.getSelectedFile().getName();
theOutString = fc.getSelectedFile().getPath();
System.out.println("You chose to open this file: " + theOutFile);
jTextField1.setText(theOutString);
}
}
private String theOutString;
- 12-05-2007, 06:24 PM #2
Make the JFileChooser a member variable and instantiate it only one time then it will remember where to appear and the last user–selected directory.
Java Code:class Pseudo { JFileChooser fc; Pseudo() { fc = new JFileChooser("."); } jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int returnVal = fc.showOpenDialog(jButton2); ...
- 12-08-2007, 03:16 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 12
- Rep Power
- 0
But it wouldn’t remember where to appear each time you run this application would it? just while you are running it?
I ended up using a file reader and write to save a "cookie" type file, to remember the location, then try to read it in, if successful (Location2 != null)
PHP Code:if (Location2 != null){ fc.setCurrentDirectory(new File(Location2)); } int returnVal = fc.showOpenDialog(jButton2); if(returnVal == JFileChooser.APPROVE_OPTION) { theOutString = fc.getSelectedFile().getName(); theOutString = fc.getSelectedFile().getPath(); jTextField1.setText(theOutString); }
- 12-08-2007, 05:17 PM #4
But it wouldn’t remember where to appear each time you run this application would it? just while you are running it?
Right.
For persistence try Using the Preferences API.
Similar Threads
-
Localize JFileChooser
By Java Tip in forum Java TipReplies: 0Last Post: 03-14-2008, 11:54 AM -
how to use JFileChooser
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:49 PM -
how to get the location of some button
By mary in forum Java 2DReplies: 2Last Post: 08-05-2007, 04:02 AM -
how to have function variables remember their values between calls
By asterik123 in forum New To JavaReplies: 2Last Post: 08-03-2007, 04:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks