tags:

views:

106

answers:

2

When a JFrame is in a maximized state, the getBounds method returns bounds that match its current state. What I need is the "restore bounds" (that is, the size/location of what the window would be set to if a user clicked on "Restore Down" on a Windows platform) without actually setting it to a non-maximized state.

The reason I ask is because I wish to save the bounds as a preference and then when the user reopens the application, set the window to the saved bounds (and then maximize it if that is what the last state was).

I considered adding a listener for when a window is maximized but it seems to me that the listener is notified after its maximized and I'll need to be notified right before it is maximized.

A: 

I considered adding a listener for when a window is maximized but it seems to me that the listener is notified after its maximized and I'll need to be notified right before it is maximized.

That should work. Add a listener to handle component resized. When the event is fired, before updating the saved bounds, you check to make sure that the window is not in its maximized state.

camickr
+1  A: 

I store the extendedState, window location and frame size in the user preferences in my application. Adding a ComponentListener is the way to go. You will have to listen to the componentMoved, componentResized, and componentShown methods. When the value returned from frame.getExtendedState == JFrame.NORMAL, store the Rectangle from frame.getBounds() as your non-maximized value. If getExtendedState() return JFrame.MAXIMIZED_BOTH, then don't store the current bounds, but do store the fact that it is extended. As long as you only store the value from the last JFrame.NORMAL extended state you will be good.

Nemi