Is it possible to make jFace TreeViewer specific node support bold font for its LabelProvider?
Asked Answered
K

2

0

I'm developing a tree based on TreeViewer and created myLabelProvider which extends LabelProvider and then use viewer.setLabelProvider(new ViewLabelProvider());, but all the nodes are still displayed with normal plain style.

Code:

class ViewLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider{

    public String getText(Object obj) {
        return obj.toString();
    }

    public Image getImage(Object obj) {
        return null;
    }

    public Font getFont(Object element, int columnIndex) {
        FontRegistry registry = new FontRegistry();
        return registry.getBold(Display.getCurrent().getSystemFont().getFontData()[0].getName());
    }

    public Image getColumnImage(Object element, int columnIndex) {
        return null;
    }

    public String getColumnText(Object element, int columnIndex) {
        return null;
    }
}

References:

http://www.eclipsezone.com/eclipse/forums/t95078.html
How do I make part of a TreeViewer cell bold?

Can anyone give some advice?

Karb answered 27/3, 2014 at 6:2 Comment(2)
Are you using multiple columns or a single column?Conal
@Conal No columns. It's just a hierarchy tree like the Package Explorer.Karb
C
1

If you are just using a single column then make your label provider implement IFontProvider (and perhaps IColorProvider)

public Font getFont(Object element) {
   return font or null to use default
}

Note: If you create any fonts be sure to dispose of them when your are done.

Conal answered 27/3, 2014 at 7:45 Comment(0)
H
0

Maybe

public String getColumnText(Object element, int columnIndex)
{
 if (columnIndex == 0)
  return getText(element);
 return null;
}
Haruspex answered 27/3, 2014 at 6:18 Comment(3)
the 3 methods getFont getColumnImage getColumnText is never called in my labelProviderKarb
Do you have columns in your TreeViewer? Is it constructed by TreeViewer(Tree tree) constructor, where tree has several columns? If no, maybe you need just IFontProviderHaruspex
I don't have columns in my tree. It's just a hierarchy tree like the Package Explorer tree.Karb

© 2022 - 2024 — McMap. All rights reserved.