views:

144

answers:

1

I have a TableViewer widget with a single column, where each row in the table's contents is a Composite with a number of child widgets. The column has a label provider that is a subclass of OwnerDrawLabelProvider. The label provider is supposed to render the Composite children in the table cell, but when paint() is called, nothing is rendered.

I have found a number of examples on how to draw plain or styled text items, or primitives using the GC in the Event object passed in, but nothing on drawing the contents of a Composite in the cell's area.

Is this possible, and if so, what am I doing wrong?

Here is the code to create the table:

    viewer = new TableViewer(container, SWT.NONE);
    final Table table = viewer.getTable();

    TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.LEFT);
    TableColumn tableColumn = viewerColumn.getColumn();
    tableColumn.setText(Messages.getString("column.header.name"));
    tableColumn.setResizable(true);
    tableColumn.setMoveable(false);
    tableColumn.setWidth(500);
    viewerColumn.setLabelProvider(new CompositeLabelProvider());

Here are the measure and paint methods for the custom label provider, CompositeLabelProvider:

    @Override
    protected void measure(Event event, Object element) {

        CompositeTableItem row = rowMap.get(element);
        Composite contents = row.getContents(viewer.getTable().getParent());
        Point size = contents.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        event.setBounds(new Rectangle(event.x, event.y, size.x, size.y));
    }

    @Override
    protected void paint(Event event, Object element) {

        CompositeTableItem row = rowMap.get(element);
        Composite contents = row.getContents(viewer.getTable().getParent());

        contents.redraw(event.getBounds().x, event.getBounds().y, event.getBounds().width, event.getBounds().height, true);

        contents.update();
    }
+1  A: 

I think OwnerDrawLabelProvider is meant to be used if you want to draw your own component using the GC. If you only want render a normal SWT widget in a table you should probably use TableEditor and #setEditor(Control) to set the control you want to show in the table cell.

Kire Haglin
Thanks! The TableEditor did the trick.
Critical Failure