tags:

views:

204

answers:

2

I want to display a Java FileDialog and sets its initial size to something reasonable (it doesn't show the maximize button, and a lot of users might not know you can use alt-space X instead). I tried setBounds but it didn't do anything, and I don't see a method for maximizing it; what am I missing?

Update: on Windows at least, the system file dialog remembers size and maximize state even across program invocations, so the user can set it to the desired size just once and it will stay that way; that seems sufficient, so I will leave it at that.

+1  A: 

You need to call pack() and then setSize(). Until it's packed, it's not realized. Do the pack before setVisible(). BTW, pack() will size the dialog so that the contents get their preferred size, so that might be what you want.

I use an AWT FileDialog, and have had no problems with size (I have had problems with trying to get it to be located properly, but that's a separate issue). My code is just the obvious:

dlg=new FileDialog(owner,"Select File to Load",FileDialog.LOAD);
dlg.setFile(null);
dlg.setVisible(true);

The default size is perfectly reasonable in my experience. And of course it's resizable with the mouse - since it's a dialog, and not a frame it's not maximizable.

I just tried a few variations on setSize() after my dlg.setVisible(), and it does completely ignore them, perhaps because it's a heavyweight window with it's own ideas about it's size and location. I must admit I have always been surprised that the FilenameFilter doesn't function in Windows, which sounds like an odd limitation.

Software Monkey
I tried pack(), but no effect.
rwallace
+1  A: 

First of all, I'd recommend you use JFileChooser from swing (unless you are restricted to awt for some reason). It got a lot more options.

Second, they both inherit from Component which has a setsize method. You can get the size of the user's screen and set the size to a percentage of that.

Do you absolutly want to maximise it ? If so just set the size to the size of the user's screen.

You can get the screen size with : Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

If this doesn't answer your question, please be more precise.

Silence
For the love of all things good in this world, please do not suggest people maximize windows. My resolution is 2560 x 1600.
Dave Jarvis
lol ! Well I provide only the means. Not the good sense needed to use it well. ;) But I could take out the part about maximizing. He was the one to bring it up!
Silence