Link to home
Start Free TrialLog in
Avatar of bjg
bjg

asked on

JFileChooser question

I am using a JFileChooser to select a directory.  The problem is when I select the directory and then click on the approve button, nothing happens.  Here is the code I am using:

_fileChooser = new JFileChooser(System.getProperty("user.dir"));
_fileChooser.setApproveButtonText("Select");
_fileChooser.setDialogTitle("Select a directory");
_fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
_fileChooser.setFileFilter(new DirFileFilter());

class DirFileFilter extends FileFilter
    {
        public boolean accept(File f)
        {
            return f != null && f.isDirectory();
        }
               
        public String getDescription()
        {
            return "Directory";
        }
    }


protected void showFileChooser()
    {
        System.out.println("showFileChooser()");
        System.out.println("isDirectorySelectionEnabled = " + getFileChooser().isDirectorySelectionEnabled());
        int result = getFileChooser().showOpenDialog(this);
        System.out.println("result = " + result);
        if (result == JFileChooser.APPROVE_OPTION)
        {
            File f = getFileChooser().getSelectedFile();
            System.out.println("Selected directory = " + f);
        }
    }

What am I doing wrong?
Avatar of txholdem
txholdem

Have you tried this? replacing the APPROVE_OPTION block:

public void approveSelection(){
  File file = getSelectedFile();
  System.out.println("Selected directory = " + f);
}
Avatar of bjg

ASKER

I don't think you understand what is happening, when I click on the select button, NOTHING happens ... meaning the file dialog does not go away ... absolutely NOTHING happens.
'void approveSelection()' works for me just fine. When I click on the select button of the filechooser, it executes what I put in the handler (approveSelection).
ASKER CERTIFIED SOLUTION
Avatar of txholdem
txholdem

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bjg

ASKER

So you are saying override public void approveSelection()?
Avatar of bjg

ASKER

I guess I am just not understanding what you are getting at ... when the user clicks on the approve button on the JFileChooser, I want the dialog to go away and the selected file should be set so when I call filechooser.getSelectedFile(), it returns the file (directory that I selected).
yes, override public void approveSelection().

public void approveSelection(){
  File f = getSelectedFile();
  System.out.println("Selected directory = " + f);
  System.exit(0);
}
Hi,
       I think that the problem is with your getFileChooser() method.if the code you posted in the beggning is the code in the method then probably you are returning a new instance of the FileChooser object.

ie if yur method is :-

private FileChooser getFileChooser()
{
     fileChooser = new JFileChooser     (System.getProperty("user.dir"));
_fileChooser.setApproveButtonText("Select");
_fileChooser.setDialogTitle("Select a directory");
_fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
_fileChooser.setFileFilter(new DirFileFilter());
return _fileChooser;
}

so every call to this returns a new instance.

If you have a condtion to check whether the _fileChooser already exists it mifht work

private FileChooser getFileChooser()
{
   if(_fileChooser == null)
  {
     fileChooser = new FileChooser                  System.getProperty("user.dir"));
      _fileChooser.setApproveButtonText("Select");
      _fileChooser.setDialogTitle("Select a directory");
     _fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     _fileChooser.setFileFilter(new DirFileFilter());
  }
   
return _fileChooser;
}

Regards,
Rahul.
Avatar of bjg

ASKER

I do have a conditional check to see if it is null before creating a new instance of it, so that's not the problem ...
Avatar of bjg

ASKER

As for your suggesstion txholdem, I don't think that is the solution because I don't think that approveSelection is ever being called, otherwise the dialog box would disappear because the default implementation of approveSelection calls setVisible(false)
Hi bjg,

  I'm not sure I can help, but could I clarify something please?

  This looks very similar to a problem I encountered recently.  I found that the approveSelection wasn't being called if there were no files in the directory.  If I selected a directory that did contain files, then it worked OK.  Could you confirm that this is the behaviour you are seeing?

  Thanx.

  Jim.
Avatar of bjg

ASKER

This is not the behavior I am seeing, it never calls approveSelection(), even if there are any files in the directory.
Hi,

  OK.  I'll see if I can replicate the problem in another way.

  Thanx.

  Jim.
This question appears to have been abandoned. In fact, I will include links to ALL your open questions below:
 
Your options are:
1.  Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3.  Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4.  Delete the question. Again, you must tell the other participants why you wish to do this.
 
For special handling needs, please post a zero point question in the link below, include the question QID/link.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:  Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues.
https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
 

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:
 
Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
 
Thank you everyone.
 
Open Qs:

https://www.experts-exchange.com/jsp/qShow.jsp?qid=11466298
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11480198
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20081509
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20101105
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107120
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20119910
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20137332
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20165238
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20269609
 
 
Moondancer
Moderator @ Experts Exchange
 
P.S.  For year 2000 question, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
 
 
Force-accepted by
Netminder
CS Moderator