• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to deal with a JFileChooser  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there !

I'm doing a little program here, and I'm having a hard time controlling the default buttons that the JFileChooser provides. I'm talking about The Open and Cancel buttons in my case(for the Open File Dialog)

Basically I need the code to do the following:

1.- Once I select the file I desire to be opened and then click the 'Open' button, the dialog(JFileChooser) should be destroyed (as If I invoked dispose() on it)

2.- Once I click the 'Cancel' Button the dialog(JFileChooser) should be destroyed (as If I invoked dispose() on it).

Now, there's one more condition; I would like to have both functionalities in the same method (Is it possible?) -- Inside something like this:



Please help me....
Thanks in advance,

Jose
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose, been awhile. I could write this code for you but that is not what you are asking. Answer is yes, but we could get some complaints from the small-tight-code department.

What you do is write a switch of some kind, it can be if()else but the matching of elses with the if's is tedious and error prone. The event usually has some sort of numeric, though recently I saw a String datatype in Layout managers where we usually see ints. It's sorta involved, I am assigning you because of where you are at in asking the question to invest as much as five hours trying to figure it out before reposting.

It's skill building that you will need if you go on. Answer to your question is yes, it can be done.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Nicholas!

Actually I'm asking for a bit of code help if possible, because as far as I know the Buttons(Open/Cancel) for the JFileChooser aren't as explicit as I had expected, So, I don't know which are the ACTUAL variables to work with... Other than that, the logic is pretty simple I'm assuming..

Thank you Nicholas!

Good Luck!

Jose
 
Rob Spoor
Sheriff
Posts: 21094
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The show methods showDialog, showOpenDialog and showSaveDialog each return an int. This can be JFileChooser.CANCEL_OPTION, JFileChooser.APPROVE_OPTION or JFileChooser.ERROR_OPTION - see the API for more info.

Now what people mostly do is something like this:
Before this code you can have all the shared code you want, including setting up file filters etc.

This will happen if you use the standard functionality. You can override the button behaviours by overriding the approveSelection and cancelSelection methods. For instance, the following will allow you to ask for overwriting the file before actually closing the dialog (tested):
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob,

Fortunately the behavior for the JFileChooser seems to be really straight forward, Once I reviewed some examples, I figured what was I doing wrong. But thanks for completing my swing knowledge.

You're my savior, my personal Jesus Christ. and I see now you're a bartender too, Congrats Mr. as always Keep up the fantastic work!
Your have always been Awesome responses !

Thank you,

your friend,
Jose
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!