2013-07-12 55 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 
     } 
     }     
    } 
    }); 

它的工作爲我好,但是,更大的數據沒有經過測試。