R Sumesh Manjunath
R Sumesh Manjunath

Reputation: 75

Place the label in the top SWT

I have two labels placed in the gridLayout. label 1 is just one word and the label 2 is 4 lines. Since the label 2 is 4 lines, label 1 is verticaly centered, but I want that to be vertically in the top.

Below is the label settings I have used.

Label label = new Label(parent, SWT.WRAP);
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false);
    gd.widthHint = 200;
label.setLayoutData(gd);

Kindly help me in placing the label alignment on top not in the center for label 1

Upvotes: 1

Views: 592

Answers (1)

Baz
Baz

Reputation: 36894

If you look at the Javadoc of GridData.HORIZONTAL_ALIGN_BEGINNING and GridData.VERTICAL_ALIGN_BEGINNING, you can see that it says:

Not recommended. Use new GridData(SWT.BEGINNING, int, boolean, boolean) instead.

and

Not recommended. Use new GridData(int, SWT.BEGINNING, boolean, boolean) instead.

Always use the SWT alignment constants:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, false));

    Label left = new Label(shell, SWT.BORDER);
    left.setText("LEFT");
    Label right = new Label(shell, SWT.BORDER);
    right.setText("RIGHT\nRIGHT\nRIGHT\nRIGHT");

    GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
    left.setLayoutData(data);
    data = new GridData(SWT.FILL, SWT.FILL, true, true);
    right.setLayoutData(data);

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

Looks like this:

enter image description here

Upvotes: 2

Related Questions