tags:

views:

510

answers:

1

I am using GridLayout in my SWT GUI app. I have the following GridData defined for each grid cell. The grid cell itself is just a label.

    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.heightHint = 25;
    gridData.widthHint = 25;
    gridData.verticalAlignment = GridData.VERTICAL_ALIGN_CENTER;
    gridData.verticalIndent = 10;

And I create each lable element like this -

    Label l = new Label(shell, SWT.BORDER);
    l.setAlignment(SWT.CENTER);
    l.setText("some text");
    l.setLayoutData( gridData );

Now My problem is in spite of using verticalAlignment property, verticalIndent property and setAlignment on the label itself, I am not able to get the text align vertically centered with respect to the grid cell area ( 25 x 25 ). I think I am missing something. How can I achieve the vertically centered alignment in a grid cell ?

+2  A: 

There is no setVerticalAlignment() on a Label and if you set the height, using the heightHint in the LayoutData, that becomes the size of the control and effectively stopping you from setting it in the middle. A workaround would be to put the Label into a composite with the size you want and then center the control. E.g.

Composite labelCell = new Composite(shell, SWT.BORDER);
GridData gridData = new GridData();
gridData.heightHint = 250;
gridData.widthHint = 250;
labelCell.setLayoutData(gridData);
labelCell.setLayout(new GridLayout());
Label l = new Label(labelCell , SWT.NONE);
l.setText("some text");
l.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
Kire Haglin