| Author |
JFileChooser modification
|
perza metasta
Greenhorn
Joined: May 03, 2011
Posts: 2
|
|
I did this
JFileChooser selectFolderDialog = new JFileChooser ();
selectFolderDialog.setApproveButtonText("Select");
and expected to see a file chooser with text "Select" in the approve
button. However, it was still "Save".
What went wrong?
br, perza
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Welcome to the Ranch!
I tried it as well, and oddly enough it ignores the explicitly set value. So I checked the look&feel UI class (for the metal loo&feel that's javax.swing.plaf.metal.MetalFileChooserUI), but that uses BasicFileChooserUI.getApproveButtonText(JFileChooser) and that returns the JFileChooser's getApproveButtonText() value unless that's null - which it isn't because we've just set it.
So I did some testing with the UI:
Output:
Approve button text: Open
Approve button text: Hello
Approve button text: Goodbye
Approve button text: Goodbye
Approve button text: Goodbye
Approve button text: Goodbye
Odd enough. The text is retrieved correctly, but then ignored. So I checked the source of JFileChooser.showSaveDialog:
Next up: JFileChooser.setDialogType:
And there's the culprit: setApproveButtonText(null). So, apparently we're not allowed to overwrite the approve button text for open or save dialogs!
The solution: don't use showOpenDialog or showSaveDialog - use showDialog. By setting the dialog type to JFileChooser.OPEN_DIALOG (which is the default) it will behave like an open dialog as usual. Change it to JFileChooser.SAVE_DIALOG to use it as a save dialog.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
perza metasta
Greenhorn
Joined: May 03, 2011
Posts: 2
|
|
Thanks a lot Rob, now I got it working!
br, perza
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You're welcome.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
Some nice detective work . . . It would have been better not to have that side-effect, or if they insist on having it, saying something in the documentation.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
|
Nice work Rob.
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
@Rob.
Just wow!
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Well, it annoys me to not know why something doesn't work like you expect to, so I try to find the cause. In this case the problem intrigued me so much I just had to find the cause and solution.
|
 |
 |
|
|
subject: JFileChooser modification
|
|
|