How do I make part of a TreeViewer cell bold?

java, jface, swt

Solution

You probably need to use a label provider based on `StyledCellLabelProvider`.

`DelegatingStyledCellLabelProvider` is easiest to use because you only need to provide a label provider implementing `DelegatingStyledCellLabelProvider.IStyledLabelProvider`. The key method in this is

public StyledString getStyledText(Object element);

which lets you just return a `StyledString` for each object.

Problem

I currently want to write an Eclipse editor based on a JFace `TreeViewer`. I added a `CellLabelProvider` to the `TreeViewer`. If I set the font of a cell with directly in the `update` method of the `CellLabelProvider` to a bold font the label is shown bold. But I want only part of the label to be shown as bold. So I apply `StyleRange`s to the cell. Selected colors in the 'StyleRange's work perfectly, but setting the font of a `StyleRange` to a bold one, does not seem to work. Why is that and how do I fix it?

Original source