简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM