简体   繁体   中英

SWT TreeViewer with combobox and checkbox

I'm looking for a way to display a tree in the first column of a table/grid with three other columns, one with a combobox and the others with checkboxes. I've been trying to make this work with a TreeViewer but it doesn't quite fit what I'm looking for. The tree goes together fine. The Combobox column where I used the EditorSupport for the column and return a ComboboxCellEditor in the getCellEditor method but you can only see that there is a combobox in the column when you select a cell in that column. Then when you click out of the cell the selected value goes back to the default blank. The same goes for the checkbox columns where it is is only visible when the cell is selected. I'm looking for something that will display my tree with the combobox column and checkbox columns always visible. I've looked at TableViewer but couldn't find a way to force in a tree in the first column. I've looked at Nebula Grid but that doesn't look like it supports comboboxes. Any tips on how to get one of these to work like what I am looking for or if there is some other tree/table/grid I should be looking at. Thanks.

Edit: Here's the code for the EditingSupport class.

private class ComboBoxEditingSupport extends EditingSupport
{
    private ComboBoxCellEditor cellEditor;

    public ComboBoxEditingSupport(ColumnViewer viewer)
    {
        super(viewer);

        cellEditor =
            new ComboBoxCellEditor(((TreeViewer) viewer).getTree(),
                new String[] {
                        "Some String",
                        "Some other String" }, SWT.READ_ONLY);

    }

    @Override
    protected CellEditor getCellEditor(Object element)
    {
        if (element instanceof MyObject
        {
            return cellEditor;
        }
        return null;
    }

    @Override
    protected boolean canEdit(Object element)
    {
        if (element instanceof MyObject
        {
            return true;
        }
        return false;
    }

    @Override
    protected Object getValue(Object element)
    {
        return 0;
    }

    @Override
    protected void setValue(Object element, Object value)
    {
        TreeItem[] ti = treeViewer.getTree().getSelection();
        CCombo combo = ((CCombo) cellEditor.getControl());
        String str = combo.getItem(combo.getSelectionIndex());
        ti[0].setText(1, str);
    }
}

Your setValue method must update the value in your model data (the data returned by your content provider). The element parameter to setValue is the particular model data object ( MyObject ) that you should update.

After updating the value call:

getViewer().update(element, null);

to get the tree to update the display from the model.

Trying to update the TreeItem directly will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM