jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes JFileChooser modification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JFileChooser modification" Watch "JFileChooser modification" New topic
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: 20077
    
  30

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 - OCEEJBD 6 - OCEJPAD 6
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: 20077
    
  30

You're welcome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43890
    
  32
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: 3982
    
  18

Nice work Rob.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10744
    
    9

@Rob.
Just wow!


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20077
    
  30

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.
 
Have you checked out Aspose?
 
subject: JFileChooser modification
 
It's not a secret anymore!