没有选择的SWT表

时间:2014-07-24 10:24:15

标签: java swt selection

我正在寻找一种能够完全禁用选择突出显示的解决方案。我有以下方法:

table.addListener(SWT.Selection, new Listener()
{
    @Override
    public void handleEvent(Event event)
    {
        event.detail = SWT.NONE;
        event.type = SWT.None;
        event.doit = false;
        try
        {
            table.setRedraw(false);
            table.deselectAll();
        }
        finally
        {
            table.setRedraw(true);
        }
    }
});

但不知怎的,只有一半解决了我的要求。背景突出显示确实消失了,但选择周围的矩形仍然显示:

enter image description here

如果你更精确地看一下矩形,你会发现它看起来很丑,特别是在复选框周围。这实际上是我想要禁用选择的主要原因。

1 个答案:

答案 0 :(得分:3)

您可以强调关注的另一个Widget不是您的Table。通过这样做,你将松开虚线(代表焦点)。

以下是一个例子:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, true));

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Focus catcher");

    final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);

    for (int col = 0; col < 3; col++)
        new TableColumn(table, SWT.NONE).setText("Col " + col);

    for (int i = 0; i < 10; i++)
    {
        TableItem item = new TableItem(table, SWT.NONE);

        for (int col = 0; col < table.getColumnCount(); col++)
            item.setText(col, "Cell " + i + " " + col);
    }

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

    table.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            table.deselectAll();

            button.setFocus();
            button.forceFocus();
        }
    });

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

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}
相关问题