aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFrame size and location problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame size and location problems" Watch "JFrame size and location problems" New topic
Author

JFrame size and location problems

Bob Little
Greenhorn

Joined: Jun 14, 2009
Posts: 4
Hello,

I have a swing app that uses JFrame as it's main window. It contains a JPanel with several JTabbedPaneS and lots of components. The app reads a ResourceBundle containing the last size and location. During the WindowEvent.WINDOW_CLOSING event, I want to poll the JFrame's current size and location, then save it in the ResourceBundle file.

After I load up all the tabs, panels, components etc., and invoke the JFrame.pack() method, the JFrame size is not what was recorded in the properties file, which is as expected. But if I re-poll the window size and position later when the app is closing, the size is different than originally reported even if I don't resize the main window.


ResourceBundle at app start:


Main window init:


Window close method:


I clearly don't understand something about the behavior of pack() or the Swing framework.

I'm unsure of how the JFrame.pack() figures out its padding so I can adjust for it at app close. Also I don't know why the getBounds() method would report two different sets of values - even if no change has been made to that JFrame.

Any advice would be helpful.

Thanks
-bob
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
> But if I re-poll the window size and position later when the app is closing, the size is different than originally reported even if I don't resize the main window.

Post your SSCCE that demonstrates this behaviour.
Bob Little
Greenhorn

Joined: Jun 14, 2009
Posts: 4
Rob Camick wrote:> But if I re-poll the window size and position later when the app is closing, the size is different than originally reported even if I don't resize the main window.

Post your SSCCE that demonstrates this behaviour.


Thanks for the reply Rob.
I haven't been able to reproduce this on a smaller scale to 100% satisfaction. But I've at least seen something that gave me a clue.

When the following code is compiled and run, the behavior is as I'm expecting. If the call to setBounds is commented out and the code recompiled and rerun, the window width and height after pack differs from when windowClosing is invoked. This tells me that I've created the behavior I've described through some incorrect combinations of object construction and size setting methods (or lack thereof). I'm thinking that as a result, Swing is left to calculate the size and location based on some defaults, which may or may not be what I am expecting.

In my full application, I was pretty sure I had called setBounds on all the components I'm using, but I cannot be sure right now.
Being that I have a ton of components spread out over several tabs, its probable I haven't done something right somewhere.


So this topic can be considered solved. Cause: Loose Nut Behind Keyboard.
-bob

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame size and location problems