GridBagConstraints anchor dilemma

From: Gary V (auc_at_worldnet.att.net)
Date: 10/29/04


Date: 29 Oct 2004 11:30:45 -0700

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


Quantcast