Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFileChooser modification

 
perza metasta
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
perza metasta
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob, now I got it working!

br, perza
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47262
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice work Rob.
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob.
Just wow!
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic