aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFrame not resizing when JPanel changes size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame not resizing when JPanel changes size" Watch "JFrame not resizing when JPanel changes size" New topic
Author

JFrame not resizing when JPanel changes size

Vas Nesterov
Greenhorn

Joined: Dec 11, 2012
Posts: 2
Hi everyone. I am currently working on a game - Sokoban. I ran into a bit of an issue, where I want the frame to automatically resize itself when a new level begins (as most levels are different in size).
I can't seem to get it to work.
Here are parts of my code:



The game is essentially a JPanel that I draw inside a JFrame.
As you can see, I've been trying to revalidate the JFrame in different parts, but it doesn't appear to be working. I think it's revalidating the JPanel. How can I revalidate the JFrame to match the new coordinates of the JPanel?

Thanks!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You should instantiate your frame from within another class, not the panel class. That's a very bad approach. In this case, revalidate() refers to panel.

Did you try to invoke pack() on your frame after you change/add new panel? That should resize it to fit its content.


The quieter you are, the more you are able to hear.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
have you tried calling
f.pack();
when new level starts?

(will need class level 'f')
Vas Nesterov
Greenhorn

Joined: Dec 11, 2012
Posts: 2
Great! Calling pack() at level starts worked. I had to make my JFrame class level as well as static.
I removed revalidate from my code.

Thank you very much!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JFrame not resizing when JPanel changes size