如何在 SWT 中实现单击时突出显示整行并在双击时编辑单元格的表格?

huangapple go评论66阅读模式
英文:

SWT how to have table that highlights entire row with single click, and edits cell with double click?

问题

SWT新手在此。所以,我的要求是能够突出显示整行,同时能够选择多行,并使双击编辑单元格。是否需要FocusCellManager?相关的代码片段:

EditorActivationEvent

final ColumnViewerEditorActivationStrategy actSupport = 
   new ColumnViewerEditorActivationStrategy(this)
   {
     @Override
     protected boolean isEditorActivationEvent
     (ColumnViewerEditorActivationEvent event)
       {
         return event.type == 
             ColumnViewerEditorActivationEvent.TRAVERSAL
             || event.eventType == 
         ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
             || event.eventType == 
             ColumnViewerEditorActivationEvent.KEY_PRESSED
             || event.eventType ==
             ColumnViewerEditorActivationEvent.PROGRAMMATIC;
       }
  };

TableViewerEditor的创建

TableViewerEditor.create(this,
    mgr,
    actSupport,
    ColumnViewerEditor.TABBING_HORIZONTAL|
    ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR|
    ColumnViewerEditor.TABBING_VERTICAL|
    ColumnViewerEditor.KEYBOARD_ACTIVATION);

关于mgr(focusCellManager)的代码:

focusCellOwnerDrawHighlighter drawHighlighter = new FocusCellOwnerDrawHighlighter(this);

final TableViewerFocusCellManager mgr = new TableViewerFocusCellManager(this, null);

tableViewer(不在前面的代码片段中出现,因为tableViewer是由另一个类扩展的,我们使用另一个类,所以不想让您感到困惑):

TableViewer vwr = new TableViewer(tableComposite,SWT.BORDER|SWT.FULL_SELECTION|SWT.MULTI);

请注意,这些代码片段看起来是用Java编写的,用于创建一个SWT表格视图,实现了突出显示整行、选择多行和双击编辑单元格的功能。

英文:

SWT newbie here. So, what I want is to be able to highlight a whole row, along with being able to select multiple rows, and make it so that double click edits the cells. Is a focusCellManager necessary? Relevant pieces of code:

EditorActivationEvent

final ColumnViewerEditorActivationStrategy actSupport = 
   new ColumnViewerEditorActivationStrategy(this)
   {
     @Override
     protected boolean isEditorActivationEvent
     (ColumnViewerEditorActivationEvent event)
       {
         return event.type == 
             ColumnViewerEditorActivationEvent.TRAVERSAL
             || event.eventType == 
         ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
             || event.eventType == 
             ColumnViewerEditorActivationEvent.KEY_PRESSED
             || event.eventType ==
             ColumnViewerEditorActivationEvent.PROGRAMMATIC;
       }
  };

creation of TableViewerEditor

TableViewerEditor.create(this,
    mgr,
    actSupport,
    ColumnViewerEditor.TABBING_HORIZONTAL|
    ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR|
    ColumnViewerEditor.TABBING_VERTICAL|
    ColumnViewerEditor.KEYBOARD_ACTIVATION);

code for mgr (focusCellManager):

focusCellOwnerDrawHighlighter drawHighlighter = new FocusCellOwnerDrawHighlighter(this);

final TableViewerFocusCellManager mgr = new TableViewerFocusCellManager(this, null);

The tableViewer (doesn't appear in the previous snippers as tableViewer is extended by another class and we use the other class, so I don't want to confuse you):

TableViewer vwr = new TableViewer(tableComposite,SWT.BORDER|SWT.FULL_SELECTION|SWT.MULTI);

答案1

得分: 1

在表格列上使用EditingSupport,结合以下TableViewerEditor似乎适合我:

TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);

ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(viewer) {
  @Override
  protected boolean isEditorActivationEvent(final ColumnViewerEditorActivationEvent event) {
    return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
        || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
        || event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
  }
};

int feature = ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_HORIZONTAL
    | ColumnViewerEditor.KEYBOARD_ACTIVATION
    | ColumnViewerEditor.TABBING_CYCLE_IN_VIEWER;

TableViewerEditor.create(viewer, actSupport, feature);
英文:

Using EditingSupport on the table columns combined with the following TableViewerEditor seems to work for me:

TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);

ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(viewer) {
  @Override
  protected boolean isEditorActivationEvent(final ColumnViewerEditorActivationEvent event) {
    return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
        || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
        || event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
  }
};

int feature = ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_HORIZONTAL
    | ColumnViewerEditor.KEYBOARD_ACTIVATION
    | ColumnViewerEditor.TABBING_CYCLE_IN_VIEWER;

TableViewerEditor.create(viewer, actSupport, feature);

huangapple
  • 本文由 发表于 2020年8月7日 12:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定