Results 1 to 4 of 4
  1. #1
    Mr tuition is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default 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;

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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);
                ...

  3. #3
    Mr tuition is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    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);
    				}

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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

  1. Localize JFileChooser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:54 AM
  2. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:49 PM
  3. how to get the location of some button
    By mary in forum Java 2D
    Replies: 2
    Last Post: 08-05-2007, 04:02 AM
  4. Replies: 2
    Last Post: 08-03-2007, 04:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •