Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RowLayout in SWT that vertically aligns on the font baseline

Tags:

java

fonts

swt

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).

demo screenshot

like image 239
Phantrast Avatar asked Mar 21 '23 01:03

Phantrast


1 Answers

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();
}
like image 135
Baz Avatar answered Apr 01 '23 03:04

Baz