Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the restore bounds of a JFrame when it is in a maximized state

Tags:

java

swing

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.

like image 871
Avrom Avatar asked Dec 29 '22 13:12

Avrom


1 Answers

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() returns 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.

like image 155
Nemi Avatar answered May 19 '23 15:05

Nemi