Danny Lo
Danny Lo

Reputation: 1583

SWT Table without selection

I am looking for a solution which disables the selection highlighting completely. I've got the following approach:

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);
        }
    }
});

But it's somehow only half-solved my requirement. The background highlighting is gone indeed, but the rectangle around the selection still appears:

enter image description here

And if you look at the rectangle more precisely, you will see that it looks ugly especially around the checkbox. This is in fact the main reason why I want do disable the selection.

Upvotes: 1

Views: 3217

Answers (1)

Baz
Baz

Reputation: 36884

You can force the focus on another Widget that's not your Table. By doing that, you'll loose the dotted line (which represents the focus).

Here is an example:

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();
}

Upvotes: 3

Related Questions