Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame, JPanel and sizes  RSS feed

 
Mark van de Veerdonk
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'ld like to add a JPane to a JFrame. The JFrame is empty but the JPane is already filled with components and already has a certain size. I'ld like to add the pane to the frame but the pane has to maintain its size. How do I know what size to make the frame to not resize the panel?

Is this possible without needing complex code for retrieving the sizes of border, insets, title bars and such? My idea was to:
1) remember the size of the panel
2) set the pane to that size (which is too small)
3) add the panel to the frame's content pane. This should resize the panel as the layout manager will make it fill the entire content pane.
4) Retrieve the panel's new size and determine the difference from its original size.
5) Use the found difference to enlarge the frame with thereby restoring the frame's size.
The panel should have its original size now without needing to know what kind of other elements are taking up space. Unfortunately I've nog been able to get this working.

I notice that setting the bounds of the frame doesn't resize the frame's content pane instantly. Even not after a call to doLayout(). Same thing with adding the pane to the frame. This is not behavior I expected. Please help!

Thanks,
Mark
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a fan of letting components calculate or recalculate their sizes
as need be, but you could try:
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would be the method in the code above that makes it do what you're asking:


pack

public void pack()

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
 
Mark van de Veerdonk
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks!! It works like a breeze.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!