TableViewer 中字段之间的 Tab 键

发布于 2024-08-06 19:07:30 字数 1257 浏览 1 评论 0原文

我想做的是能够在表中的元素之间进行切换。

我目前正在创建这样的表。

this.tableViewer = 
            new TableViewer(parent , SWT.FULL_SELECTION);

   tableViewer.setUseHashlookup(true);
        table = tableViewer.getTable();

        GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.grabExcessVerticalSpace = true;
        table.setLayoutData(gridData);

        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        ...

   /** Create the Cell Editor Array - will hold all columns **/
        editors = new CellEditor[table.getColumnCount()];
        /** Cell Editor Row 1 **/


  /** Set the column properties **/
        tableViewer.setColumnProperties(columnNames);

        /** Assign the cell editors to the viewer **/
        tableViewer.setCellEditors(editors);

        /** Set the cell modifier for the viewer **/
        tableViewer.setCellModifier(new MyCellModifier(this));
        //Create the Table Viewer

        /** Table Viewer Content and Label Provider **/
        tableViewer.setContentProvider(new MyContentProvider(this));
        tableViewer.setLabelProvider(new MyLabelProvider());

但我不知道如何设置表格。其他一切都可以编辑列、显示数据等。只是停留在最后一部分。

如果我错过了明显的文档或 javadocs - 我很抱歉,甚至指出这些都会很棒。

What I'd like to do is be able to tab between elements in table.

I currently am creating my table like this.

this.tableViewer = 
            new TableViewer(parent , SWT.FULL_SELECTION);

   tableViewer.setUseHashlookup(true);
        table = tableViewer.getTable();

        GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.grabExcessVerticalSpace = true;
        table.setLayoutData(gridData);

        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        ...

   /** Create the Cell Editor Array - will hold all columns **/
        editors = new CellEditor[table.getColumnCount()];
        /** Cell Editor Row 1 **/


  /** Set the column properties **/
        tableViewer.setColumnProperties(columnNames);

        /** Assign the cell editors to the viewer **/
        tableViewer.setCellEditors(editors);

        /** Set the cell modifier for the viewer **/
        tableViewer.setCellModifier(new MyCellModifier(this));
        //Create the Table Viewer

        /** Table Viewer Content and Label Provider **/
        tableViewer.setContentProvider(new MyContentProvider(this));
        tableViewer.setLabelProvider(new MyLabelProvider());

But I'm not sure how to set up the tabulation. Everything else works as far as editing columns, showing data, etc. Just stuck on this last part.

If I've missed obvious documentation or javadocs - my apologies and even pointing to those would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小兔几 2024-08-13 19:07:30

尽管发布的解决方案 thehiatus 级别非常低并且可能会起作用(我还没有测试它),但 JFace 为您提供了解决此特定问题的框架。请参阅 org.eclipse.jface.viewers.TableViewerFocusCellManager 和 org.eclipse.jface.viewers.CellNavigationStrategy 类来解决此问题。

Although the solution thehiatus posted is very low level and will probably work (I haven't tested it), JFace gives you a framework for this specific problem. See the org.eclipse.jface.viewers.TableViewerFocusCellManager along with org.eclipse.jface.viewers.CellNavigationStrategy classes to solve this problem.

提笔书几行 2024-08-13 19:07:30

我认为默认情况下选项卡不会在 swt 表中从一个单元格跳转到另一个单元格。相反,它会遍历到下一个控件。因此,您还需要告诉它在按下 Tab 时不要遍历。

KeyListener keyListener = new KeyLisener()
{
    public void keyPressed(KeyEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
        {
            // There are numerous setSelection methods.  I'll leave this to you. 
            tableViewer.getTable().setSelection(...)
        }
    }

    public void keyReleased(KeyEvent evt){}
}

TraverseListener traverseListener = new TraverseListener()
{
    public void keyTraversed(TraverseEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
            evt.doit = false;
    }
}

tableViewer.getTable().addKeyListener(keyListener);
tableViewer.getTable().addTraverseListener(traverseListener);

此外,正如 derBiggi 建议的那样,需要将侦听器添加到 Table 对象,而不是 TableViewer。

I think by default tab does not jump from cell to cell in an swt table. Instead it traverses to the next control. So you'll also need to tell it not to traverse when tab is pressed

KeyListener keyListener = new KeyLisener()
{
    public void keyPressed(KeyEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
        {
            // There are numerous setSelection methods.  I'll leave this to you. 
            tableViewer.getTable().setSelection(...)
        }
    }

    public void keyReleased(KeyEvent evt){}
}

TraverseListener traverseListener = new TraverseListener()
{
    public void keyTraversed(TraverseEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
            evt.doit = false;
    }
}

tableViewer.getTable().addKeyListener(keyListener);
tableViewer.getTable().addTraverseListener(traverseListener);

Also, as derBiggi suggested, the listeners need to be added to the Table object, not the TableViewer.

却一份温柔 2024-08-13 19:07:30

我无法使用 TraverseListener 获得所需的行为(它不会在表内遍历),并且无法使其与 FocusCellManager一起使用CellNavigationStrategy。我终于找到了这个解决方案,它使我能够在一行中从一列切换到另一列并自动激活编辑器。

Viewer viewer =  ...

TableViewerFocusCellManager focusCellManager =
    new TableViewerFocusCellManager(
        viewer,
        new FocusCellHighlighter(viewer) {});

ColumnViewerEditorActivationStrategy editorActivationStrategy =
    new ColumnViewerEditorActivationStrategy(viewer) {

            @Override
            protected boolean isEditorActivationEvent(
                ColumnViewerEditorActivationEvent event) {
                    ViewerCell cell = (ViewerCell) event.getSource();
                   return cell.getColumnIndex() == 1 || cell.getColumnIndex() == 2;
            }

};

TableViewerEditor.create(viewer, focusCellManager, editorActivationStrategy,
    TableViewerEditor.TABBING_HORIZONTAL);

I couldn't get the desired behavior with a TraverseListener (it would not traverse within the table), and I had trouble getting it to work with a FocusCellManager and CellNavigationStrategy. I finally found this solution that enables me to tab from column to column within a row and automatically activate the editor.

Viewer viewer =  ...

TableViewerFocusCellManager focusCellManager =
    new TableViewerFocusCellManager(
        viewer,
        new FocusCellHighlighter(viewer) {});

ColumnViewerEditorActivationStrategy editorActivationStrategy =
    new ColumnViewerEditorActivationStrategy(viewer) {

            @Override
            protected boolean isEditorActivationEvent(
                ColumnViewerEditorActivationEvent event) {
                    ViewerCell cell = (ViewerCell) event.getSource();
                   return cell.getColumnIndex() == 1 || cell.getColumnIndex() == 2;
            }

};

TableViewerEditor.create(viewer, focusCellManager, editorActivationStrategy,
    TableViewerEditor.TABBING_HORIZONTAL);
原野 2024-08-13 19:07:30

您需要添加一个 KeyListener 并将选择或焦点设置到下一个单元格:

tableViewer.getTable().addKeyListener(new KeyListener(){

   public void keyPressed(KeyEvent e) {

       System.out.println("Key Pressed");
       if (e.keycode == SWT.TAB)
       {
           System.out.println("Detected TAB key");
           // set table viewer selection
       }
   }

   public void keyReleased(KeyEvent e) {

       System.out.println("Key Released");
   }}
);

You need to add a KeyListener and set the selection or focus to the next cell:

tableViewer.getTable().addKeyListener(new KeyListener(){

   public void keyPressed(KeyEvent e) {

       System.out.println("Key Pressed");
       if (e.keycode == SWT.TAB)
       {
           System.out.println("Detected TAB key");
           // set table viewer selection
       }
   }

   public void keyReleased(KeyEvent e) {

       System.out.println("Key Released");
   }}
);
彡翼 2024-08-13 19:07:30

我还必须在表中的元素之间实现制表符。我们使用 Nebula 的 Grid 作为表格。
首先,我必须抑制焦点的切换,以防止其移出表格。
输入图片此处的描述

然后我添加了一个关键侦听器,它将焦点/选择移动到下一个单元格:

在此处输入图像描述

我还制作了自己的算法,将选择的单元格向右移动一个单元格,当位于行末尾时,将其移动到下一行的开头。当到达表格末尾时,选择将移回表格中的第一个单元格。

这为我解决了问题。

I also had to implement tabbing between elements in a table. We use Grid from Nebula as the table.
Firstly, I had to suppress tabbing the focus preventing it from moving out of the table.
enter image description here

and then I added a Key Listener which moves the focus/selection to the next cell:

enter image description here

I also made my own algorithm to move the selection one cell to the right and when at the end of the row, move it to the beginning of the next row. When end of table is reached, the selection moves back to the first cell in the table.

This solved the problem for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文