This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes FileDialog not working!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "FileDialog not working!!" Watch "FileDialog not working!!" New topic
Author

FileDialog not working!!

Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Hi guys,

I am trying to use a java.awt.FileDialog to let the user save a file. But I cannot figure out how to receive the event when the user presses "Save" or "Cancel".

I tried registering a WindowListener to catch when the dialog closes, and then retrieve the file name. But it never calls the listener




When I run this program and press the "test" button, it pops open the save dialog. It calls the windowOpened() callback.

But it never calls any of the callbacks after the user enters the filename and hits "save".

How do I get notified of this event???

Thanks
Geoffrey


Sun Certified Programmer for the Java 2 Platform
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
looks like an 'in progress' bug (since Jan 97)

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4028889
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Thanks Michael, so this means FileDialog is pretty useless to me then!!

Is there any other way to get the filename when the user presses "Save"?

Geoffrey
[ January 24, 2007: Message edited by: Geoffrey Falk ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Is there a reason you have to use awt and can't use Swing? Then you could use the JFileChooser. I've never had a problem with it.
[ January 24, 2007: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
The problem with JFileChooser is that it has tended to track the native look-and-feel poorly. I try to use AWT FileDialog whenever I can, and fall back to JFileChooser when I have too, which unfortunately is fairly often. I wish that the powers that be would put some work into modernizing FileDialog.


bitguru blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42910
    
  68
I'm not quite following what the listeners are supposed to do. That's not how FileDialog works. The call to setVisible will return when the user has dismissed the dialog, either by clicking Save or Cancel. Then you can call its getFile method - if that returns null, then it was Cancel, otherwise it returns the file name (and getDirectory the directory). Am I missing something?
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Originally posted by Ulf Dittmer:
I'm not quite following what the listeners are supposed to do. That's not how FileDialog works. The call to setVisible will return when the user has dismissed the dialog, either by clicking Save or Cancel. Then you can call its getFile method - if that returns null, then it was Cancel, otherwise it returns the file name (and getDirectory the directory). Am I missing something?


OK, you are partly right and I was partly wrong.

The call to setVisible(true) returns immediately. This was my mistake.

By using show() instead, it will block until the user is done.

By using the show() method I was able to get it working. I avoided it the first time because Eclipse says that this method is deprecated (?).

Geoffrey
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

For the FileDialog I get the same behavior from .setVisible(true) and .show() - both block until the user closes the dialog in some way...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileDialog not working!!