Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame size and location problems

 
Bob Little
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic