SWT列表显示行号

时间:2013-06-12 21:41:46

标签: java list swt

如何在SWT列表中显示行号?我确实参考了一些关于显示StyledText行号的帖子,但它并没有帮助我。

提前致谢!

1 个答案:

答案 0 :(得分:2)

作为替代方案,您可以使用Table包含2个不带标题的列:

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

    Table table = new Table(shell, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    table.setLinesVisible(false);
    new TableColumn(table, SWT.RIGHT);
    new TableColumn(table, SWT.NONE);

    for (int row = 0; row < 20; row++)
    {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, row + "");
        item.setText(1, "Item " + row);
    }

    for(int col = 0; col < table.getColumnCount(); col++)
    {
        table.getColumn(col).pack();
    }

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

看起来像这样:

enter image description here