实现JTree工具提示的最佳方法是什么?

时间:2008-11-07 13:53:49

标签: java swing tooltip jtree

自JTree& TreeModel不提供直接开箱即用的工具提示,您认为什么是为JTree提供特定于项目的工具提示的最佳方式?

编辑:(之后回答我自己的问题。)

@Zarkonnen:感谢getTooltipText的想法。

我发现了覆盖DefaultTreeCellRenderer并考虑分享它的另一种方式(可能还是更好一点):

public class JTreeWithToolTips {
    private static class OwnRenderer extends DefaultTreeCellRenderer {
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean sel, boolean expanded, boolean leaf, int row,
                boolean hasFocus) {
            setToolTipText("foobar" + row);
            return super.getTreeCellRendererComponent(tree, value, sel,
                    expanded, leaf, row, hasFocus);
        }
    }

    public static void main(String[] args) {
        JTree tree = new JTree(new Object[] { "foo", "bar", "foobar" });
        tree.setCellRenderer(new OwnRenderer());
        ToolTipManager.sharedInstance().registerComponent(tree);

        JFrame frame = new JFrame();
        frame.getContentPane().add(tree);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

3 个答案:

答案 0 :(得分:15)

请参阅JTree上的getTooltipText。这应该允许您根据树中悬停的内容显示工具提示。 (但是,请阅读文档,您需要使用ToolTipManager注册JTree。)

答案 1 :(得分:1)

是的,你可以使用onMouseMoved,然后使用一种方法(我不记得名字)告诉你你在哪个节点结束。如果你得到null,那么显然你不是在一个节点上。

答案 2 :(得分:0)

在处理特定的TreeNode子类时,根据您自己的答案和评论,我想出了一个用于TreeNode实现的界面。

请注意我们如何检查value TooltipableTreeCellRenderer是否属于public static interface Tooltipable { public String getToolTip(); } public static class TheNode extends DefaultMutableTreeNode implements Tooltipable { private String shortDesc, longDesc; public TheNode(String shortDesc, String longDesc) { super(); this.shortDesc = shortDesc; this.longDesc = longDesc; } @Override public String getToolTip() { return longDesc; } @Override public String toString() { return shortDesc; } } public static class TheModel extends DefaultTreeModel { public TheModel() { super(new TheNode("Root", "The base of everything")); TheNode root = (TheNode)getRoot(); root.add(new TheNode("Second", "I am a number two")); TheNode node = new TheNode("Third", "Another one bites the dust"); root.add(node); node.add(new TheNode("Last", null)); // No tooltip for this one } } public static class TreeTooltipRenderer extends DefaultTreeCellRenderer { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { if (value instanceof Tooltipable) setToolTipText(((Tooltipable)value).getToolTip()); return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(100, 100, 300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTree tree = new JTree(new TheModel()); ToolTipManager.sharedInstance().registerComponent(tree); tree.setCellRenderer(new TreeTooltipRenderer()); frame.add(new JScrollPane(tree), BorderLayout.CENTER); frame.setVisible(true); }

wp_nav_menu