• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Problem with JFileChooser

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thejaswini thanks a lot i got my answer...........
reply
    Bookmark Topic Watch Topic
  • New Topic