Search code examples
javaswtccombo

How to display CCombo combo box inside SWT Table


I am not able to populate a CCombo box inside a SWT Table. When a new TableItem is added, it should get added with CCombo in the first column. Please find the code below. Thanks in advance!

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

Solution

  • It looks like the code snippet you've referenced is this: TableCellEditorComboTextButton

    When iterating over the items and creating the CCombo objects, don't forget to tell the editor to grab the available horizontal 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);
    }
    

    With that change you should see a CCombo in each row in the "FACEBOOK_MESSAGE" column.


    Full code:

    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