想要在 SWT 的 TreeColumn 中创建下拉列表(组合框查看器)

want to create drop down list (Combo box viewer) in TreeColumn in SWT

我正在使用树,在这棵树中我有一个五树列。还创建两个 treeItem,一个是父项,另一个是子项,通过编程将它们的值放在树列中。现在我需要在每个树列(第一个除外)中的下拉列表(组合框)来查看列表数据。当前仅获取单个值。请查看以下代码以获取可在 treecolumn 中编辑的树项值。

private void editTreeTable(final Tree table){
     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 = 1; 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 );
                    }
                }
            }

        });

}

现在找到下面的代码从 API

中获取树项值
private void createTestSuiteTable( final Tree table) 
{      
       //Dispose all elements
    TreeItem items[] = table.getItems();
    for(int i=0;i<items.length;i++)
    {
        items[i].dispose();
    }
    TSGson tsGsons[] = TestSuiteAPIHandler.getInstance().getAllTestSuites();
    boolean checked=false;
    for (TSGson tsGson : tsGsons) 
    {
        parentTestSuite = new TreeItem(table, SWT.NONE|SWT.MULTI);
        parentTestSuite.setText(new String[] { "" +tsGson.tsName, "", "","","","" });
        parentTestSuite.setData("EltType","TESTSUITE");

        if(tsGson.tsTCLink==null)
                continue;

        for(TSTCGson tsTCGson : tsGson.tsTCLink) 
        {
            TreeItem trtmTestcases = new TreeItem(parentTestSuite, SWT.NONE|SWT.MULTI);
            trtmTestcases.setText(new String[] {tsTCGson.tcName, 
                    tsTCGson.tcParams.get(0)!=null ?tsTCGson.tcParams.get(0).tcparamValue:"",
                    tsTCGson.tcParams.get(1)!=null ?tsTCGson.tcParams.get(1).tcparamValue:"",
                    tsTCGson.tcParams.get(2)!=null ?tsTCGson.tcParams.get(2).tcparamValue:"",
                    "local",
                    tsTCGson.tcParams.get(4)!=null ?tsTCGson.tcParams.get(4).tcparamValue:"" });
            trtmTestcases.setData("EltType","TESTCASE");

            table.setSelection(parentTestSuite);
            if(checked)
            {
                  trtmTestcases.setChecked(checked);
            }
        }

    }
}

在 SWT 中找到以下用于创建树列的代码

localHostTable = new Tree(composite_2,SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
localHostTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
localHostTable.setLinesVisible(true);
localHostTable.setHeaderVisible(true);

TreeColumn trclmnNewColumn_1 = new TreeColumn(localHostTable, SWT.NONE);
trclmnNewColumn_1.setWidth(113);
trclmnNewColumn_1.setText("TestSuite/TestCase");

TreeColumn trclmnColumn_5 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_5.setWidth(73);
trclmnColumn_5.setText("Exe_Platform");


TreeColumn trclmnColumn_6 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_6.setWidth(77);
trclmnColumn_6.setText("Exe_Type");

TreeColumn trclmnColumn_7 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_7.setWidth(85);
trclmnColumn_7.setText("Run_On");

TreeColumn trclmnColumn_8 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_8.setWidth(81);
trclmnColumn_8.setText("Thread-Count");

final TreeColumn trclmnColumn_9 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_9.setWidth(97);
trclmnColumn_9.setText("Column5");

请推荐

由于您的问题中没有关于 ComboCCombo 控件的内容,因此我无法帮助您解决问题。我也不打算为您编写代码,但我可以尝试通过一个简短的示例为您指明正确的方向。

Yes, i want the combo to always be visible.

您仍然可以使用 TreeEditor 来完成此操作,它实际上比您使用 MouseListener 发布的代码片段更简单。

创建 CCombo (or Combo) as you would in any other situation, and use TreeEditor.setEditor(...) 方法以指定 CCombo 控件应显示在该单元格中:

// ...
final CCombo combo = new CCombo(tree, SWT.NONE);
final TreeEditor editor = new TreeEditor(tree);
editor.setEditor(combo, item, 1);
// ...

完整 MCVE:

public class TreeComboBoxTest {

    private final Display display;
    private final Shell shell;

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

        final Tree tree = new Tree(shell, SWT.BORDER | SWT.VIRTUAL | SWT.FULL_SELECTION);
        tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        tree.setLinesVisible(true);
        tree.setHeaderVisible(true);

        final TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
        column1.setWidth(75);
        column1.setText("Column 1");

        final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
        column2.setWidth(75);
        column2.setText("Column 2");

        final TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(0, "Hello");

        final CCombo combo = new CCombo(tree, SWT.NONE);
        combo.setItems(new String[] { "Item 1", "Item 2", "Item 3" });

        final TreeEditor editor = new TreeEditor(tree);
        editor.setEditor(combo, item, 1);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;

        // Optional, but allows you to get the current value by calling
        // item.getText() instead of going through the TreeEditor and
        // calling ((CCombo) editor.getEditor()).getText()
        combo.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                item.setText(1, combo.getText());
            }
        });
    }

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

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

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

}

注意 SelectionListener 添加到 CCombo。即使您使用了 TreeEditor,如果您调用 item.getText(index),它也会 return 一个空的 String,因为 setText(...) 还没有被调用。通过在侦听器中调用 setText(...),您不必通过 TreeEditor 来获取值。

所以你可以调用 item.getText(index) 而不是 ((CCombo) editor.getEditor()).getText()