Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing pack() on a Window un-maximizes it, how to avoid that?

I've a window and since I dinamically change it's children (sometimes I swap JPanels), I found no other solution than calling pack() on the window to get the new element displayed. Otherwise it will show up only if I resize manually the window.

The problem with this is that the if the window is maximized, after pack() it won't be anymore, which is not what I could give to the client.

Any clues?

like image 974
gotch4 Avatar asked Aug 29 '11 15:08

gotch4


2 Answers

First of all, I hope that you're using CardLayout for panel swapping, since this functionality is built into that particular layout manager. And typically, you'll want to invoke validate/revalidate and repaint on the container to refresh the display.

See also:

  • How to Use CardLayout
like image 198
mre Avatar answered Oct 03 '22 09:10

mre


If you really have to:

int state = frame.getExtendedState();
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

... Otherwise @mre's solution is (much) better! :)

like image 30
dacwe Avatar answered Oct 03 '22 11:10

dacwe