Re: AWT/Swing layout behavior?
Peter Duniho wrote:
I am having trouble figuring out the right way to use the GridBagLayout
class.
I have some sample code (see below) that I _expected_ would take the two
components added to the frame and always make them the same size,
arranged vertically.
What happens instead is that when one of the two components is a Panel
or JPanel (and possibly other container types...those are the two I
tested), if that component contains another component, it takes up more
than half of the frame.
Reading the documentation, I am under the impression that the
GridBagLayout class automatically allocates the dimensions of a given
component according to the weight assigned to it in the
GridBagConstraints class set for the component. Thus, since these two
components have the same weights, they should always be equally sized.
But for some reason, having the container actually contain something
affects this.
The sample code only adds one item, and the difference in size is
noticeable but not very large. However, this came up in a more complex
situation, where a larger number of controls are added. In that case,
the size difference is proportionally larger as well, resulting in
effective relative weights more like 3 or 4 to 1.
Is there a way to get the GridBagLayout class, or some other built-in
LayoutManager, to do this sort of auto-sizing where the relative weights
will always actually stay equal?
Thanks!
Pete
p.s. Something I ran into while putting the sample code together is
that sometimes when I'd run the code, the button would not show up.
It'd be briefly visible, and it would show up again once the window is
resized. But to start with, it's not visible. Is there some basic flaw
in the way I'm creating this UI that is leading to this erratic
behavior? Or am I just looking at some sort of run-time bug?
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TestPanel
{
/**
* @param args
*/
public static void main(String[] args)
{
Frame frame = new Frame("TestPanel");
GridBagLayout grid = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel panel;
frame.setLayout(grid);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.weightx = 1;
constraints.weighty = 1;
panel = new JPanel();
panel.setBorder(new LineBorder(Color.BLACK));
panel.add(new Button("Test"));
frame.add(panel);
grid.setConstraints(panel, constraints);
panel = new JPanel();
panel.setBorder(new LineBorder(Color.BLACK));
frame.add(panel);
grid.setConstraints(panel, constraints);
frame.setSize(240, 480);
frame.setVisible(true);
}
}
I didn't try your program but I'll give you a couple of clues. The
weights only effect extra space. Once you figure out what that actually
means, using GBL becomes much easier. It is usually easier to pack the
container than to set it to a particular size. With GBL if you put more
into the container than will fit, the layout manager will squash some or
all of the components to invisibility. You can make that happen by
resizing your container smaller. GBL is pretty good at following
preferred size, so setting that on your components can help you layout
your GUI.
If you want a grid style layout manager that makes all components the
same size you can use GridLayout. If you are using it for controls it
is pretty ugly but for containers in containers it is a nice simple
layout manager.
I should really write a tutorial for GBL. It is a really powerful
layout manager but also very complex with a lot of subtle features.
--
Knute Johnson
email s/nospam/knute/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem