0

I would like to add a button on some rows of my TreeViewer. To do that, I’ve used a method that I saw on a forum and it was working on a TableViewer.

I‘ve implemented my own Label provider on the column where I want the button to be. So I’ve overriden the class update(ViewerCell cell) which calls my method addButton(cell):

(I have simplified the code for a better comprehension)

public class SelectVariableButtonLabelProvider extends ColumnLabelProvider {

@Override
public void update(ViewerCell cell) {
    if(...){
       addButton(cell);
    }
}

private void addButton(ViewerCell cell) {
    TreeItem item = (TreeItem) cell.getItem(); 
    Button btn = new Button((Composite) cell.getViewerRow().getControl(), SWT.NONE); 
    btn.setText(" select variable ");
    
    //action when the button is clicked
    btn.addListener(SWT.Selection, new SelectVariableButtonListener(tree,
            DataTypeTreeUtils.getTreeNodeDataTypeInstance(cell.getElement()), viewer));

    TreeEditor editor = new TreeEditor(item.getParent());
    editor.grabHorizontal = true;
    editor.grabVertical = true;
    // editor.horizontalAlignment = SWT.RIGHT;
    editor.minimumWidth = btn.getSize().x + 110;
    editor.setEditor(btn, item, cell.getColumnIndex());
    editor.layout();
}
}

It’s almost working. Except that the buttons of the column of buttons is duplicated when I want to extend the column. screenshot of the bug

The left “column of buttons'' : is completely working. The buttons are functional and they adapt themself to the extension of the nodes in the tree Viewer.

The right “column of buttons” : is fixed on the viewer and the buttons are not completely functional. And when I want to extend or not the nodes in the tree, the buttons are not corresponding to their rows anymore. (These are also the buttons in the foreground).

So I would like to not have the right columns which probably appeared because of a bug. I think this could be due to the composite to which the button is initialized :

Button btn = new Button((Composite) cell.getViewerRow().getControl(), SWT.NONE);

Or just because buttons are simply bugging when they are on Tree Viewers ? The same method is working on Table Viewers.

Just in case and if it helps, this is the declaration of the viewer:

viewer = new TreeViewer(treeContainer, SWT.FULL_SELECTION);
    viewer.setContentProvider(new TreeNodeTreeContentProvider());
    viewer.setLabelProvider(new CustomColumnLabelProvider());
    viewer.getTree().setHeaderVisible(true);
    viewer.getTree().setLinesVisible(true);

and this is the declaration of the column where I want to add the buttons:

//column with the buttons "select variable"
    TreeViewerColumn viewerSetValueColumn = new TreeViewerColumn(viewer, SWT.NONE);
    viewerSetValueColumn.getColumn().setWidth(110);
    viewerSetValueColumn.setLabelProvider(new SelectVariableButtonLabelProvider(viewer, getAllVariables()));

EDIT: I would like to have buttons in some rows of a column of my treeViewer. But I want them to be always visible, so I would like to avoid using editing support. I've used the LabelProvider to do it but it caused a bug (screenshot of the bug).

Does anyone know how to add buttons to a treeViewer using the labelProvider?

rosemerel
  • 1
  • 1
  • Note: On some platforms there is a fairly small limit on the number of controls you can have. A large tree with a button on every row could hit that limit. An alternative is to use `EditingSupport` with a `CellEditor` - this just uses one control activated with a cell is clicked (see for example [here](https://www.vogella.com/tutorials/EclipseJFaceTable/article.html#column-editing-support) – greg-449 Jul 02 '21 at 12:23
  • Hi greg, thank you for your answer ! In fact, I've already tried to use EditingSupport and it was working. The problem is that I wanted to have buttons always appears (with EditingSupport, they only appear when you click on the cell) – rosemerel Jul 02 '21 at 12:27

0 Answers0