简体   繁体   English

使用单列格式化SWT表

[英]Formatting a SWT table with a single column

I have the following table: 我有下表:

在此输入图像描述

What I'm wanting to do is extend the column to the size of the window without displaying the extra lines where there is no column (the cells to the right of the item column). 我想要做的是将列扩展到窗口的大小,而不显示没有列的额外行(项目列右侧的单元格)。 I'm somewhat familiar with most of SWT, but these tables are still confusing me. 我对SWT的大多数人都很熟悉,但这些表仍然令我感到困惑。

I want to be able to select an item by clicking anywhere on the row. 我希望能够通过单击行中的任意位置来选择项目。

Here is the simple UI code I used to make the above screen cap: 这是我用来制作上面屏幕截图的简单UI代码:

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

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

    TableColumn column = new TableColumn (table, SWT.NONE);
    column.setResizable(true);
    int count = 15;
    for (int i=0; i<count; i++) {
        TableItem item = new TableItem (table, SWT.NONE);
        item.setText (0, "item " + i);
    }
    column.pack();

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

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

Make your Table to be the only child of its parent and create a column with following: 使您的Table成为其父级的唯一子级,并创建一个包含以下内容的列:

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.layout.TableColumnLayout;

public class ColumnHelper {
    public static void createColumn(Table table) {
        TableColumnLayout layout = new TableColumnLayout();
        table.getParent().setLayout(layout);
        TableColumn column = new TableColumn (table, SWT.NONE);
        layout.setColumnData(column, new ColumnWeightData(10));
    }
}

If you experience a strange, growing resize behaviour of table, replace TableColumnLayout with: 如果您遇到一个奇怪的,不断增长的表调整行为 ,请将TableColumnLayout替换为:

/** Forces table never to take more space than parent has*/
public class TableColumnLayout extends org.eclipse.jface.layout.TableColumnLayout {

    @Override
    protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
        return new Point(wHint, hHint);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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