SWT 中的树中的所有树列均不可编辑

All treecolumns are not editable in a tree in SWT

我实现了一棵树,还在 SWT 中添加了 5 个 treeColumn。现在使用父 treeItem 和子 treeItem 以编程方式扩展此树。现在我想编辑所有列。当我为此编写代码时,我只得到第一列进行编辑,其余列不符合条件。请建议我如何让所有列都是可编辑的。请找到以下代码:

private void editTreeTable(final Tree table) {

    final TreeEditor editor = new TreeEditor(table);

        editor.horizontalAlignment = SWT.LEFT;

        editor.grabHorizontal = true;

        table.addSelectionListener(new SelectionAdapter() {

                public void widgetSelected(SelectionEvent e) 

                        Control oldEditor = editor.getEditor();
                        if (oldEditor != null) oldEditor.dispose();
                        TreeItem item = (TreeItem)e.item;

                        if (item == null) return;

                        // The control that will be the editor must be a 
                        Text newEditor = new Text(table, SWT.NONE);

                        newEditor.setText(item.getText());

                        newEditor.addModifyListener(new ModifyListener() {
                                public void modifyText(ModifyEvent e) {
                                        Text text=(Text)editor.getEditor();
                                        editor.getItem().setText(text.getText());
                                }
                        });
                        newEditor.selectAll();
                        newEditor.setFocus();
                        editor.setEditor(newEditor, item);`
                }
 });
}    

这里的挑战是知道选择了哪一列。实现此目的的一种方法是使用 MouseEvent 并从 MouseEvent.

的坐标获取相应的 TreeItem 和列索引

查看 Tree class,我们找到方法 getItem(Point), and the TreeItem class provides the method getBounds(int).

getItem(Point)MouseEvent 中的 Point 结合使用,然后遍历列数(参见:Tree.getColumnCount()) and passing the index to getBounds(int), we can easily identify the TreeItem and column index to provide to the TreeEditor.setEditor(Control, TreeItem, int) 方法。

例如(稍微修改您的代码并添加一些注释以进行说明):

// Use a MouseListener so that we can get the widget-relative x,y coordinates
table.addMouseListener(new MouseAdapter() {

    @Override
    public void mouseUp(final MouseEvent e) {
        final Control oldEditor = editor.getEditor();
        if (oldEditor != null) {
            oldEditor.dispose();
        }

        // Get the Point from the MouseEvent
        final Point p = new Point(e.x, e.y);
        // Get the TreeItem corresponding to that point
        final TreeItem item = table.getItem(p);
        if (item == null) {
            return;
        }
        // Now that we know the TreeItem, we can use the getBounds() method
        // to locate the corresponding column
        for (int i = 0; i < table.getColumnCount(); ++i) {
            if (item.getBounds(i).contains(p)) {
                final int columnIndex = i;
                // The control that will be the editor must be a
                final Text newEditor = new Text(table, SWT.NONE);

                newEditor.setText(item.getText(columnIndex ));

                newEditor.addModifyListener(new ModifyListener() {
                    public void modifyText(final ModifyEvent e) {
                        final Text text = (Text) editor.getEditor();
                        editor.getItem().setText(columnIndex , text.getText());
                    }
                });
                newEditor.selectAll();
                newEditor.setFocus();
                // Set the editor for the matching column
                editor.setEditor(newEditor, item, columnIndex );
            }
        }
    }

});

完整 MCVE:

public class TreeEditorTest {

    private final Display display;
    private final Shell shell;

    public TreeEditorTest() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new FillLayout());

        final Composite baseComposite = new Composite(shell, SWT.NONE);
        baseComposite.setLayout(new GridLayout());
        baseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final Tree table = new Tree(baseComposite, SWT.BORDER | SWT.FULL_SELECTION);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final int columnWidth = 100;
        new TreeColumn(table, SWT.NONE, 0).setWidth(columnWidth);
        new TreeColumn(table, SWT.NONE, 1).setWidth(columnWidth);
        new TreeColumn(table, SWT.NONE, 2).setWidth(columnWidth);
        new TreeColumn(table, SWT.NONE, 3).setWidth(columnWidth);
        new TreeColumn(table, SWT.NONE, 4).setWidth(columnWidth);

        final String[] strings = new String[] { "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        final TreeItem parent = new TreeItem(table, SWT.NONE);
        parent.setText(strings);

        final TreeItem child1 = new TreeItem(parent, SWT.NONE);
        child1.setText(strings);
        final TreeItem child2 = new TreeItem(parent, SWT.NONE);
        child2.setText(strings);

        parent.setExpanded(true);

        final TreeEditor editor = new TreeEditor(table);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;

        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(final MouseEvent e) {
                final Control oldEditor = editor.getEditor();
                if (oldEditor != null) {
                    oldEditor.dispose();
                }

                final Point p = new Point(e.x, e.y);
                final TreeItem item = table.getItem(p);
                if (item == null) {
                    return;
                }
                for (int i = 0; i < table.getColumnCount(); ++i) {
                    if (item.getBounds(i).contains(p)) {
                        final int columnIndex = i;
                        // The control that will be the editor must be a
                        final Text newEditor = new Text(table, SWT.NONE);

                        newEditor.setText(item.getText(columnIndex ));

                        newEditor.addModifyListener(new ModifyListener() {
                            public void modifyText(final ModifyEvent e) {
                                final Text text = (Text) editor.getEditor();
                                editor.getItem().setText(columnIndex , text.getText());
                            }
                        });
                        newEditor.selectAll();
                        newEditor.setFocus();
                        editor.setEditor(newEditor, item, columnIndex );
                    }
                }
            }

        });

    }

    public void run() {
        shell.setSize(600, 200);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    public static void main(final String... args) {
        new TreeEditorTest().run();
    }

}