2014-07-24 51 views
1

我正在尋找一個完全禁用選擇高亮的解決方案。我有以下的方法:沒有選擇的SWT表

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

但它以某種方式只解決了我的要求。背景高亮的確是走了,但周圍的選擇矩形仍出現:

enter image description here

如果你在矩形看起來更準確地說,你會看到,它看起來醜陋特別是圍繞複選框。這實際上是我想要禁用選擇的主要原因。

回答

3

你可以強調另一個Widget這不是你的Table。通過這樣做,你將失去虛線(代表焦點)。

下面是一個例子:

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

感謝。我創建了一個隱形按鈕'invisibleButton = new Button(table,SWT.NONE); invisibleButton.setVisible(false);'並將焦點重定向到它,如果需要'invisibleButton.setFocus(); invisibleButton.forceFocus();(它比你的例子稍微複雜一些,因爲其中一列是可編輯的)。但爲什麼樣式選項SWT.NO_FOCUS沒有做它應該做的事? –

+0

@DanyloEsterman很多風格位都只是提示。操作系統可能會尊重他們或現在可能。 – Baz

+0

我還有一個小問題。我真的需要重置'event'對象的所有字段嗎? –