【问题标题】:Highlighting swt table row on focus lost在焦点丢失时突出显示 swt 表行
【发布时间】:2017-04-24 01:33:40
【问题描述】:

我面临着这里提到的同样的问题 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());
        }

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

【问题讨论】:

  • 你能不能把focusGained的内容去掉,看看行有没有颜色。
  • 刚刚在我家的 linux 机器上试了一下。即使Table 失去焦点,该行仍然突出显示。您可以添加您的问题的屏幕截图吗?
  • 解决了这个问题 看看代码是否更正:
  • 如果您找到了解决方案,请将其作为答案发布,而不是在评论中。如果您想为您的问题添加信息edit它。
  • 我认为无法回答,因为这个问题是我自己提出的。我没有得到这个问题的答案窗口。我可以添加有问题的解决方案吗?

标签: java eclipse-plugin swt eclipse-rcp


【解决方案1】:

可以使用下面的sn-p:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2016-04-07
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多