2013-07-12 58 views
0

我正面临着这里提到的同样问题SWT: Table lost selection。我使用的是Ubuntu 12.04 NOT windows。即使聚焦丢失后,是否有任何方法突出显示SWT表的选定行。我尝试将焦点侦听器添加到表格中并且焦点丢失了我更改了选定的项目背景颜色,并且焦点增益重置了背景颜色。看代码。突出显示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()); 
     } 

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

+0

你可以删除'focusGained'的内容,看看这行是否有颜色。 – Baz

+0

刚刚在我的linux机器上尝试过它。即使“表格”失去焦点,该行仍然突出显示。你可以添加你的问题的截图吗? – Baz

+0

解决了此问题请参阅代码更正: –

回答

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

它的工作为我好,但是,更大的数据没有经过测试。