简体   繁体   中英

SWT: veritcal separator too tall; horizontal too short; table too narrow

I'm a SWT newbie and am having some trouble wrangling my UI to look like I want it to. Consider the following code, cobbled together from the SWT snippets:

    Display.setAppName("App Name");

    Display display = new Display();

    Shell shell = new Shell(display);
    GridLayout shellLayout = new GridLayout();
    shellLayout.numColumns = 1;
    shell.setLayout(shellLayout);

    Group group = new Group(shell, SWT.DEFAULT);
    RowLayout groupLayout = new RowLayout();
    group.setLayout(groupLayout);

    for (int i = 0; i < 3; i++)
        new Button(group, SWT.PUSH).setText("ABC" + i);

    new Label(group, SWT.SEPARATOR | SWT.VERTICAL);

    for (int i = 0; i < 3; i++)
        new Button(group, SWT.PUSH).setText("ABC" + i);

    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);

    Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    table.setLayoutData(gridData);

    for (String title : new String[] { "A", "B", "C", "D", "E", "F", "G" })
        new TableColumn(table, SWT.NONE).setText(title);

    for (int i = 0; i < 128; i++) {
        TableItem item = new TableItem (table, SWT.NONE);
        item.setText(0, "123");
        item.setText(1, "234");
        item.setText(2, "345");
        item.setText(3, "456");
        item.setText(4, "567");
        item.setText(5, "678");
        item.setText(6, "789");
    }

    for (TableColumn column : table.getColumns())
        column.pack();

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

    while (shell.isDisposed() != true)
        if (display.readAndDispatch() != true)
            display.sleep();

    display.dispose();

When run, the resulting display has the following issues:

  1. The vertical separator is about twice as "tall" as the buttons. The top row in the grid layout looks like this:
ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3
               |

I'd like to get the separator to be only as tall as the buttons.

  1. The horizontal separator is about 1/10 the width of the screen. How can I get it to expand to the entire width of the row in the grid?

  2. The table's columns are only as wide as the text they contain. This causes the table's width to be about 1/2 the width of the window. I'd like the table's width (and by extension the width of the columns) to grow and shrink to match whatever the window's width is. Can any point point me in the right direction for how to accomplish that?

The horizontal label needs to be told to grab more space;

Label temp = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
temp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

The call to column.pack() is what is changing the column width to match the data. If you want to set them up to explicit sizes, you need to actually set attributes on your Column objects instead of just creating them with new Column. Getting all columns to dynamically resize with the window and table requires writing a ResizeListener for the table. Otherwise the columns tend to stay the same regardless of table size.

The height of a vertical separator can be set using layout data.

Button button = null;
for (int i = 0; i < 3; i++) {
    button = new Button(group, SWT.PUSH);
    button.setText("ABC" + i);
}

Label verticalSeparator = new Label(group, SWT.SEPARATOR | SWT.VERTICAL);
RowData layoutData = new RowData();
layoutData.height = button.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
verticalSeparator.setLayoutData(layoutData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM