This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com