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

时间:2018-10-09 07:31:29

标签: java swt ccombo

我无法在SWT CCombo内填充Table框。添加新的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); 
}

1 个答案:

答案 0 :(得分:0)

您所引用的代码段看起来像是:TableCellEditorComboTextButton

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

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();
    }

}

enter image description here

相关问题