P
Peter Duniho
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);
}
}
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);
}
}