RowLayout in SWT that vertically aligns on the font baseline

fonts, java, swt

Solution

`RowLayout` has a field called `center`. Just set it to `true` and you're good.

`center` specifies whether the controls in a row should be centered vertically in each cell for horizontal layouts, or centered horizontally in each cell for vertical layouts. The default value is `false`.

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);

    RowLayout layout = new RowLayout();
    layout.center = true;

    shell.setLayout(layout);
    shell.setText("StackOverflow");

    new Label(shell, SWT.NONE).setText("Label");
    new Button(shell, SWT.PUSH).setText("Button");

    shell.pack();
    shell.open();

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

    display.dispose();
}

Problem

SWT's RowLayout is fine for the purpose, however I'd like the vertical alignment on the font baseline for all widgets containing text. Is there maybe a ready custom Layout that does this? Snippet204.java probably has what it takes to implement it, but I just want to ask if it has been done before. Couldn't find it, but it seems like a typical requirement. In the screenshot's top, there's the row layout, below is a fixed layout that I manually arranged to the baseline (WindowBuilder has snapping for that).

Original source