File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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: 19792
    
  20

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
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: 19792
    
  20

You're welcome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
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: 3649
    
  17

Nice work Rob.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    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: 19792
    
  20

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.
 
Don't get me started about those stupid light bulbs.
 
subject: JFileChooser modification