views:

324

answers:

2

I am attempting to use a Java AWT FileDialog, but I want to replace the default Java Dialog icon with something else. In short, the code looks something like this:

Frame frame = new Frame();
Image image = ImageIO.read(new URL("file:/path/to/myfile.jpg"));
FileDialog fileDialog = new FileDialog(frame, "Save As", FileDialog.SAVE);

fileDialog.setIconImage(image);
fileDialog.setDirectory("/path/to/directory");
fileDialog.setFile("filename.txt");

fileDialog.setVisible(true);

I've tried several variation, including a different method of reading the image, packing the FileDialog, packing the Frame, setting the icon of the Frame, etc. However, regardless of what I try, the FileDialog icon never changes. When I set the icon of the Frame and set the Frame to visible, the frame displayed the correct icon, but it was still a no go for the FileDialog.

Any Thoughts?

A: 

That might not be possible on all platforms. I guess the FileDialog somehow uses the filedialog provided by the OS and if that doesn't support icons you are out of luck.

jitter
I think your comment is certainly on the right track. I just checked and FileDialogs in Windows don't default with an icon. Why in the world they default to the Java icon in Solaris is beyond me (and is quite frustrating). Thanks for the input!
Adam
A: 

FileDialog, being an AWT component, is going to be more tied to the local platform. In the Solaris case, it's possible it's picking up the icon from the java executeable that launched your app itself.

Is there a reason you can't use JFileChooser?

M1EK
The application is actually written in SWT as a Rich Client Platform application. Unfortunately the SWT file dialog provides too much uncustomizable access to the underlying file system (all that the user should be able to do is save/open a file - we cannot provide them the ability to delete files and directories). That leaves us with the option of creating our own file dialog in SWT or using the AWT file dialog. If we use the JFileChooser, we lose the native look and feel. It is an annoying paradox, but it is what it is.
Adam