I am trying line up a single column of several buttons on the left
(West) side of a container but have not been successful. Here is a
simple code example to illustrate how I am attempting this:
import java.awt.*;
public class Vertical extends Frame {
public static void main(String argv[]) {
new Vertical().show();
}
public Vertical() {
Button btn;
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = GridBagConstraints.RELATIVE;
constraints.anchor = GridBagConstraints.WEST;
for (int n = 1; n <= 8; n++) {
btn = new Button("Button " + n);
add(btn);
gridbag.setConstraints(btn, constraints);
}
resize(300, 300);
}
}
When run the buttons are lined up in the center of the Frame. Any
ideas how to anchor the buttons on the left?
Gary V
Chris Rohr - 31 Oct 2004 14:35 GMT
You need to set up a fill on your constraints. Your buttons are to the
west of its current area, but it doesn't fill across the entire space.
Just add the following constraint option:
constraints.fill = GridBagConstraints.HORIZONTAL;
Chris
> I am trying line up a single column of several buttons on the left
> (West) side of a container but have not been successful. Here is a
[quoted text clipped - 32 lines]
>
> Gary V