0

我面临着这里提到的SWT 相同的问题: Table lost selection。我使用的是 ubuntu 12.04 而不是 windows。即使在失去焦点后,有没有办法突出显示 SWT 表的选定行。我尝试将焦点侦听器添加到表中,并且在焦点丢失时我更改了所选项目的背景颜色,并且焦点增益会重置背景颜色。查看代码。

        @Override
        public void focusLost(FocusEvent e) {
            System.out.println("table focus los");
            TableItem item = fileListTable
                    .getItem(fileListTable.getSelectionIndex());
            prevSelItemBackground = item.getBackground();
            item.setBackground(soureWindow.getSelectionBackground());//Some random colour
            System.out.println(fileListTable.getSelectionIndex());
        }

        @Override
        public void focusGained(FocusEvent e) {
            System.out.println("table focus gain");
            TableItem item = fileListTable
                    .getItem(fileListTable.getSelectionIndex());
            item.setBackground(prevSelItemBackground);
            System.out.println(fileListTable.getSelectionIndex());
        }

但它不起作用。有没有其他解决方案/解决方法?

4

1 回答 1

0

可以使用以下代码段:

    this.fileListTable.addSelectionListener(new SelectionListener() {

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
        // Nothing to do
    }

    @Override
    public void widgetSelected(SelectionEvent e) {

        int selectionIndex = fileListTable.getSelectionIndex();                                                             
        TableItem[] items = fileListTable.getItems();
        TableItem newItem;
        for (int i = 0; i < items.length; i++) {
        String first = items[i].getText(0);
        String second = items[i].getText(1);
        String third = items[i].getText(2);
        items[i].dispose();
        newItem = new TableItem(fileListTable, SWT.NONE);
        newItem.setText(new String[] { first, second, third });
        if (i == selectionIndex) {
            newItem.setForeground(soureWindow.getSelectionForeground());//Or Anyother color
            newItem.setBackground(soureWindow.getSelectionBackground());//Or Anyother color
        } else {
            newItem.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));//Default foreground color
            newItem.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));//Default background color
        }
        }                
    }
    });

它对我来说工作正常,但没有在更大的数据上进行测试。

于 2013-08-02T09:17:32.390 回答