Most common frustration when designing with Layouts is that your nice pretty layout gets all screwed up because the container panel blows up to take up all the space. Even if you try a setPreferred or setMaximumSize , it seems to have no effect.
A common trick I do to avoid that is to use a wrapperPanel with its layout manager as BorderLayout. and I dump the container panel in the NORTH area of the wrapperPanel.
How BorderLayout works is that, it allocates components in the border's their preferred sizes, and all the rest of the space is filled up by CENTER. So now by adding it to NORTH, the container panel's preferred sizes come in to play, and all left over space is gobbled up by CENTER.
--
When you have a similar problem with the other components, like buttons, text boxes etc, I prefer to use GridbagLayout which automatically assigns the space equivalent to the components preferred sizes. and using GridBagConstraints you can use insets and margins, to your hearts content to layout the components perfectly. Once you understand it , GridBagLayout kicks ass.
With JTree's or I think anything that is within a JScrollPane, adjusting the preferred size of the inner component doesnt have any effect. Adjust the preferred size of the scrollpane to achieve the desired effect.
See Swing, Second Edition by Matthew Robinson, Pavel Vorobiev, published by Manning for a good explanation of GridBagLayout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment