Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Problem with JFileChooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with JFileChooser" Watch "Problem with JFileChooser" New topic
Author

Problem with JFileChooser

thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Hi everyone,

I have to implement "save" and "load" functions for some data which I am doing through two buttons "SAVE" and "LOAD". When "SAVE" is clicked some data must be stored in a file and on clicking "LOAD", the stored data must be read back from the file. I have used JFileChooser for this. But when I try to save/load and click the "Save" button or the "Open" button on the FileChooser, the chooser dialog does not close automatically. Even Cancel button has to be clicked 2-3 times for the dialog to close. Can someone pls shed some light on why this could be happening?



// TextFileFilter is used to save/open only .txt files.




Thanks in advance,
Thejaswini.
[ June 14, 2007: Message edited by: thejaswini ramesh ]
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Hey everyone,

Wrt the same pbm mentioned above, I found tht the "Load" and "Save" buttons on the panel trigger the click event more than once and thats why the fileChooser is popped up many times...

Any idea why the button click is triggered more than once?

Thanks,
Thejaswini.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
hi theJaswini!!!
As i am new to the use of this component i am unable to clear your doubt but it will be kinda of you if you help me on the below shown problem of JFileChooser.
currently i am working on a Browse Button in that i am using JFileChooser
but i am not getting,what happens if i click on the open button of JFileChooser?

What actualy i am doing is if i click on the Browse button (infront of it i have placed a empty text field),the FileChhooser should appear than after i choose a file and click on the open button the path of file must be displayed in the empty TextField .
what i think is once i click on the open Button of the file it is open to be read ? but how wil selected file path appear in the textfield?

please let me know if i am on the right track?

And one more question is the file open to be read? because at runtime i want data to be retrieved.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Any idea why the button click is triggered more than once?

usual reason is the listener is added more than once.
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Originally posted by dhwani mathur:
hi theJaswini!!!
As i am new to the use of this component i am unable to clear your doubt but it will be kinda of you if you help me on the below shown problem of JFileChooser.
currently i am working on a Browse Button in that i am using JFileChooser
but i am not getting,what happens if i click on the open button of JFileChooser?

What actualy i am doing is if i click on the Browse button (infront of it i have placed a empty text field),the FileChhooser should appear than after i choose a file and click on the open button the path of file must be displayed in the empty TextField .
what i think is once i click on the open Button of the file it is open to be read ? but how wil selected file path appear in the textfield?

please let me know if i am on the right track?

And one more question is the file open to be read? because at runtime i want data to be retrieved.


Hey Dhwani,

Hope am not late. Once you choose a file and click open int JFileChooser, the event is triggered for that button(open), but file is not opened for reading/writing. you have to handle tht in the APPROVE_OPTION event something like this:

JButton browse = new JButton("Browse");
browse.setActionListener(this);

JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

public void actionPerformed {

int returnVal = fc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String path = file.getPath(); // this can be displayed in your textfiled.
}
}

Hope this helps!
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Originally posted by Michael Dunn:
> Any idea why the button click is triggered more than once?

usual reason is the listener is added more than once.


Hi Micheal,

Thanks for you response. Yes you are right, I had added the same listener to both buttons "Save" and "Load" as below:

JButton saveLabels = new JButton("Save Labels);
JButton loadLabels = new JButton("Load Labels);

saveLabels.addActionListener(this);
loadLabels.addActionListener(this);

public void actionPerformed(ActionEvent ae) {
if(ae.getSource == saveLabel)
// save action
else if(ae.getSource == loadLabels)
// load action

}

Eventually i fixed it using a different listener for each button, but the above code also should work right? (I have used the same listener on different components quite a few times before, but never had this problem!!)

Thejaswini.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the above code also should work right?

looks OK, so should work just a single time.

do you get 2 save actions (or 2 load), or both save and load actions firing.
if both, then look for a semi-colon at the end of your if statement/s
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
With the above code, I get 2 save actions per click of save button. Similarly 2 load actions for one click of load button. I checked for semicolons after if statement and the code looks ok. Also this problem doesnt happen everytime...could it be some threading problem?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> could it be some threading problem?

unlikely.

you'll need to post a compilable/runnable program that demonstrates the problem.

copy your current code into another file, then start stripping everything
unrelated to the problem. leave only the frame, buttons, actionListener code
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
hey thejaswini thanks a lot i got my answer...........
 
 
subject: Problem with JFileChooser