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.getPro perty("use r.dir"));
_fileChooser.setApproveBut tonText("S elect");
_fileChooser.setDialogTitl e("Select a directory");
_fileChooser.setFileSelect ionMode(JF ileChooser .DIRECTORI ES_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("showFi leChooser( )");
System.out.println("isDire ctorySelec tionEnable d = " + getFileChooser().isDirecto rySelectio nEnabled() );
int result = getFileChooser().showOpenD ialog(this );
System.out.println("result = " + result);
if (result == JFileChooser.APPROVE_OPTIO N)
{
File f = getFileChooser().getSelect edFile();
System.out.println("Select ed directory = " + f);
}
}
What am I doing wrong?
_fileChooser = new JFileChooser(System.getPro
_fileChooser.setApproveBut
_fileChooser.setDialogTitl
_fileChooser.setFileSelect
_fileChooser.setFileFilter
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("showFi
System.out.println("isDire
int result = getFileChooser().showOpenD
System.out.println("result
if (result == JFileChooser.APPROVE_OPTIO
{
File f = getFileChooser().getSelect
System.out.println("Select
}
}
What am I doing wrong?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So you are saying override public void approveSelection()?
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.getSelectedFil e(), it returns the file (directory that I selected).
yes, override public void approveSelection().
public void approveSelection(){
File f = getSelectedFile();
System.out.println("Select ed directory = " + f);
System.exit(0);
}
public void approveSelection(){
File f = getSelectedFile();
System.out.println("Select
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.setApproveBut tonText("S elect");
_fileChooser.setDialogTitl e("Select a directory");
_fileChooser.setFileSelect ionMode(JF ileChooser .DIRECTORI ES_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.d ir"));
_fileChooser.setApproveBut tonText("S elect");
_fileChooser.setDialogTitl e("Select a directory");
_fileChooser.setFileSelect ionMode(JF ileChooser .DIRECTORI ES_ONLY);
_fileChooser.setFileFilter (new DirFileFilter());
}
return _fileChooser;
}
Regards,
Rahul.
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.
_fileChooser.setApproveBut
_fileChooser.setDialogTitl
_fileChooser.setFileSelect
_fileChooser.setFileFilter
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.d
_fileChooser.setApproveBut
_fileChooser.setDialogTitl
_fileChooser.setFileSelect
_fileChooser.setFileFilter
}
return _fileChooser;
}
Regards,
Rahul.
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 ...
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.
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.
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.
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.
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
Netminder
CS Moderator
public void approveSelection(){
File file = getSelectedFile();
System.out.println("Select
}