显示TableViewer不符合行,但按矩阵顺序排列

时间:2022-01-02 10:56:23

I use the TableViewer to show informations in a table. The user can select one of the shown options by selecting one line of the table.

我使用TableViewer在表中显示信息。用户可以通过选择表格的一行来选择所示选项之一。

I want to create a table in matrix form, in which the user can not only select the line. It should be possible to select every item of the table, like row 2 column 3. For every item selection an action is called to handle this item as it is in the TableViewer.

我想以矩阵形式创建一个表,用户不仅可以选择该行。应该可以选择表的每个项目,如第2行第3列。对于每个项目选择,都会调用一个操作来处​​理此项目,就像在TableViewer中一样。

As far as i now, i can add CellModifier and CellEditors to the line of Columns of the table, but the reference for the action is always the line object and not the selected TableItem.

到目前为止,我可以将CellModifier和CellEditors添加到表的列行中,但该操作的引用始终是行对象而不是选定的TableItem。

Does somebody have an example how to create such a matrix inside a Composite? I can create it by setting a GridLayout and adding the components in a for-loop, but than i get issues, when i want to redraw the Composite with new childrens. The TableViewer does already have this handling, so i dont want to implement it again.

有人有一个例子如何在Composite中创建这样的矩阵?我可以通过设置GridLayout并在for循环中添加组件来创建它,但是当我想用新的子项重绘Composite时,我会遇到问题。 TableViewer已经有了这个处理,所以我不想再次实现它。

2 个解决方案

#1


I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.

我前一段时间遇到了同样的问题,我找到解决问题的唯一方法是在与表查看器关联的SWT表窗口小部件上注册一个鼠标监听器。

MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);

public class ColumnSelectionMouseListener implements MouseListener {

    private TableColumn selectedColumn;

    @Override
    public void mouseDoubleClick(MouseEvent e) {
        // Nothing to do here
    }

    @Override
    public void mouseDown(MouseEvent e) {
        table = (Table) e.widget;
    TableItem item = table.getItem(new Point(e.x, e.y));
    for (int i = 0; i < table.getColumnCount(); i++) {
        TableColumn column = table.getColumn(i);
        Rectangle bounds = item.getBounds(i);
        if (bounds.contains(e.x, e.y)) {
            selectedColumn = column;
        }
    }
    }

    @Override
    public void mouseUp(MouseEvent e) {
        // Nothing to do here
    }

    public TableColumn getSelectedField() {
           return selectedColumn;
    }
}

Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.

然后,例如在观众的选择监听器中,您可以向鼠标监听器询问当按下鼠标时选择了哪个列,并将其与来自查看者选择的选定行组合以执行适当的操作。

Hope this can help.

希望这可以提供帮助。

Manu

#2


Maybe the following JFace snippet will help: Snippet058CellNavigationIn34

也许以下JFace片段会有所帮助:Snippet058CellNavigationIn34

Ingo

#1


I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.

我前一段时间遇到了同样的问题,我找到解决问题的唯一方法是在与表查看器关联的SWT表窗口小部件上注册一个鼠标监听器。

MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);

public class ColumnSelectionMouseListener implements MouseListener {

    private TableColumn selectedColumn;

    @Override
    public void mouseDoubleClick(MouseEvent e) {
        // Nothing to do here
    }

    @Override
    public void mouseDown(MouseEvent e) {
        table = (Table) e.widget;
    TableItem item = table.getItem(new Point(e.x, e.y));
    for (int i = 0; i < table.getColumnCount(); i++) {
        TableColumn column = table.getColumn(i);
        Rectangle bounds = item.getBounds(i);
        if (bounds.contains(e.x, e.y)) {
            selectedColumn = column;
        }
    }
    }

    @Override
    public void mouseUp(MouseEvent e) {
        // Nothing to do here
    }

    public TableColumn getSelectedField() {
           return selectedColumn;
    }
}

Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.

然后,例如在观众的选择监听器中,您可以向鼠标监听器询问当按下鼠标时选择了哪个列,并将其与来自查看者选择的选定行组合以执行适当的操作。

Hope this can help.

希望这可以提供帮助。

Manu

#2


Maybe the following JFace snippet will help: Snippet058CellNavigationIn34

也许以下JFace片段会有所帮助:Snippet058CellNavigationIn34

Ingo