hwo是否可以更改表格的默認選擇行爲,我想在用戶單擊它時選中一個單元格,並在用戶雙擊它時使其可編輯。eclipse rcp:如何在tableviewer中選擇單個單元格?
與@nonty的幫助,我得到我想要的。
這裏是我的手機熒光筆FPGA實現:
package com.amarsoft.rcputil;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
public class DefaultCellFocusHighlighter extends FocusCellOwnerDrawHighlighter {
public DefaultCellFocusHighlighter(ColumnViewer viewer) {
super(viewer);
}
protected boolean onlyTextHighlighting(ViewerCell cell) {
return false;
}
protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
}
protected Color getSelectedCellForegroundColor(ViewerCell cell) {
return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
}
protected Color getSelectedCellForegroundColorNoFocus(ViewerCell cell) {
return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
}
protected Color getSelectedCellBackgroundColorNoFocus(ViewerCell cell) {
return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
}
protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
super.focusCellChanged(newCell, oldCell);
}
}
代碼使用它:
TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tv,new DefaultCellFocusHighlighter(tv));
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(tv) {
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};
TableViewerEditor.create(tv, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
| ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);
但我得到了新的問題:
當我雙擊單元格以編輯它的價值,在細胞左側還有一點區域仍然用深藍色突出顯示
我知道爲什麼:
當使用邊框創建文本控件時,操作系統會在控件的內容周圍包含一個特定於平臺的插件。
仍然尋求固定...
@nonty謝謝。我已經檢查了這兩個片段,但我仍然不明白,在Snippet036FocusBorderCellHighlighter中,單擊單元格時,該單元格將被重繪以移除突出顯示的外觀,但同一行中的其他單元格仍然被突出顯示。我在想,可以禁用默認選擇行爲,然後編寫代碼來完成它。我的意思是當你用SWT.FULL_SELECTION創建一個表,但沒有附加任何監聽器的時候,你點擊一行,該行被選中,必須有一些鼠標事件監聽器在底層SWT表中被觸發 – CaiNiaoCoder
@nonty也許我可以刪除底層的lsitener並添加一個新的? – CaiNiaoCoder
不確定你的意思 - 當我使用FocusCellOwnerDrawHighlighter作爲FocusCellHighlighter在線158上運行示例時,我得到了預期的行爲... –