如何在 SWT 中显示 CCombo 组合框 Table

How to display CCombo combo box inside SWT Table

我无法在 SWT Table 中填充 CCombo 框。添加新的 TableItem 时,应该在第一列中添加 CCombo。请在下面找到代码。提前致谢!

GridData rightTableData = new GridData();
rightTableData.heightHint=270;
right_group_table=new Table(top_right_group, SWT.BORDER | SWT.MULTI);
right_group_table.setHeaderBackground(ApplicationColor.LIGHTGRAYBACKGROUND);
right_group_table.setLayoutData(rightTableData);
right_group_table.setLinesVisible(true);
right_group_table.setHeaderVisible(true);
right_group_table.setFont(ApplicationFont.FORMFONT);

TableColumn right_list_column = new TableColumn(right_group_table, SWT.CENTER);
right_list_column.setText(CustomString.getString("TABLE_LIST"));
right_list_column.setWidth(140);

TableColumn message_column = new TableColumn(right_group_table, SWT.NONE | SWT.DROP_DOWN);
message_column.setText(CustomString.getString("FACEBOOK_MESSAGE"));
message_column.setWidth(230);

for (int i = 0; i < contactComboList.size(); i++) {
    new TableItem(right_group_table, SWT.DROP_DOWN);
}

TableItem[] items = right_group_table.getItems();

for (int i = 0; i < items.length; i++) {
    CCombo templateDropdown = new CCombo(right_group_table, SWT.NONE);
    templateDropdown.setText("CCombo");
    templateDropdown.add("item 1");
    templateDropdown.add("item 2");
    TableEditor editor = new TableEditor(right_group_table);
    editor.setEditor(templateDropdown, items[i], 1); 
}

您引用的代码片段似乎是这样的:TableCellEditorComboTextButton

在遍历项目并创建 CCombo 对象时,不要忘记告诉编辑器获取可用的水平 space:

for (int i = 0; i < items.length; i++) {
    final CCombo templateDropdown = new CCombo(right_group_table, SWT.NONE);
    templateDropdown.setText("CCombo");
    templateDropdown.add("item 1");
    templateDropdown.add("item 2");
    final TableEditor editor = new TableEditor(right_group_table);
    editor.grabHorizontal = true; // <-- Here
    editor.setEditor(templateDropdown, items[i], 1);
}

进行此更改后,您应该会在 "FACEBOOK_MESSAGE" 列的每一行中看到一个 CCombo


完整代码:

public class ComboTableTest {

    private final Display display;
    private final Shell shell;

    public ComboTableTest() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new GridLayout());

        final Table right_group_table = new Table(shell, SWT.BORDER | SWT.MULTI);
        right_group_table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        right_group_table.setLinesVisible(true);
        right_group_table.setHeaderVisible(true);

        final TableColumn right_list_column = new TableColumn(right_group_table, SWT.CENTER);
        right_list_column.setText("TABLE_LIST");
        right_list_column.setWidth(140);

        final TableColumn message_column = new TableColumn(right_group_table, SWT.NONE | SWT.DROP_DOWN);
        message_column.setText("FACEBOOK_MESSAGE");
        message_column.setWidth(230);

        for (int i = 0; i < 10; i++) {
            new TableItem(right_group_table, SWT.DROP_DOWN);
        }

        final TableItem[] items = right_group_table.getItems();

        for (int i = 0; i < items.length; i++) {
            final CCombo templateDropdown = new CCombo(right_group_table, SWT.NONE);
            templateDropdown.setText("CCombo");
            templateDropdown.add("item 1");
            templateDropdown.add("item 2");
            final TableEditor editor = new TableEditor(right_group_table);
            editor.grabHorizontal = true;
            editor.setEditor(templateDropdown, items[i], 1);
        }

    }

    public void run() {
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

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

}