Search code examples
swteclipse-rcpjfacetreeviewer

Disable hover highlight in Jface TreeViewer on windows platform in eclipse rcp app


On windows platform, I am implementing a TreeViewer. I added listeners for SWT.MeasureItem, SWT.PaintItem, and SWT.EraseItem. In SWT.EraseItem listener, I am clearin SWT.HOT bit too. Even after that I get hover highlight of that tree item. I want to completely disable the Hover Changes in tree. How can I achieve that?

I am developing eclipse e4 rcp application. I am using StyledCellLabelProvider for tree viewer and also tried specifying COLORS_ON_SELECTION. Still no luck.


Solution

  • The following snippet works for me.

    package test;
    
    import org.eclipse.jface.viewers.TreeViewer;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Tree;
    import org.eclipse.swt.widgets.TreeItem;
    
    public class TestTreeViewer {
    
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(250, 200);
        shell.setLayout(new FillLayout());
    
        TreeViewer treeViewer = new TreeViewer(shell);
        Tree tree = treeViewer.getTree();
    
        TreeItem child1 = new TreeItem(tree, SWT.NONE, 0);
        child1.setText("1");
        TreeItem child2 = new TreeItem(tree, SWT.NONE, 1);
        child2.setText("2");
        TreeItem child3 = new TreeItem(tree, SWT.NONE, 2);
        child3.setText("3");
    
        tree.addListener(SWT.EraseItem, new Listener() {
    
            @Override
            public void handleEvent(Event event) {
    //          allow selection in tree?
    //          event.detail &= ~SWT.SELECTED;
                event.detail &= ~SWT.HOT;
            }
        });
    
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
      }
    }
    

    enter image description here