简体   繁体   English

如何在TreeCellRenderer中设置工具提示?

[英]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. 我一直坚持将工具提示设置为添加到JTree节点中的JPanel之一。 This question could be similar to JTree node's changable tooltip but not entirely. 这个问题可能类似于JTree节点的可更改工具提示,但并不完全相同。

I am also using JTree populated with some (custom) nodes. 我还使用了填充了一些(自定义)节点的JTree。 Each node contains a checkbox, a color box (JPanel) and node path. 每个节点都包含一个复选框,一个颜色框(JPanel)和节点路径。 I am implementing TreeCellRenderer. 我正在实现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? 如何为添加到节点的JPanel设置工具提示?

Have a look at the docs of JTree.getToolTipText : 看看JTree.getToolTipText的文档:

NOTE: For JTree to properly display tooltips of its renderers, JTree must be a registered component with the ToolTipManager. 注意:为了使JTree正确显示其渲染器的工具提示,JTree必须是在ToolTipManager中注册的组件。 This can be done by invoking ToolTipManager.sharedInstance().registerComponent(tree). 这可以通过调用ToolTipManager.sharedInstance()。registerComponent(tree)来完成。 This is not done automatically! 这不会自动完成!

This will fix it. 这将解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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