ParagJ
ParagJ

Reputation: 1582

How to set tooltip in TreeCellRenderer?

I am stuck with setting a tooltip to one of my JPanel added to the node in a JTree. This question could be similar to JTree node's changable tooltip but not entirely.

I am also using JTree populated with some (custom) nodes. Each node contains a checkbox, a color box (JPanel) and node path. I am implementing TreeCellRenderer. I have not posted below code for what is being added to node as I think it is not necessary.

Below is part of my code:

    public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer {

        private static final long serialVersionUID = 4025435851260573240L;

        CheckTreeSelectionModel selectionModel; 
        private TreeCellRenderer delegate; 
        TristateCheckBox checkBox = new TristateCheckBox();
        JPanel panel = new JPanel();

        public CheckTreeCellRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel){
            this.delegate = delegate;
            this.selectionModel = selectionModel;

            setLayout(new BorderLayout()); 
            setOpaque(false); 
            checkBox.setOpaque(false);
        }

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
            Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            panel.setToolTipText("Hello");
            removeAll();
        add(checkBox, BorderLayout.WEST);
        add(panel, BorderLayout.CENTER);
        add(renderer, BorderLayout.EAST);

        return this;
    }
}

How to set a tooltip for JPanel added to a node?

Upvotes: 3

Views: 950

Answers (1)

Karsten
Karsten

Reputation: 86

Have a look at the docs of JTree.getToolTipText:

NOTE: For JTree to properly display tooltips of its renderers, JTree must be a registered component with the ToolTipManager. This can be done by invoking ToolTipManager.sharedInstance().registerComponent(tree). This is not done automatically!

This will fix it.

Upvotes: 3

Related Questions