java 为 JTree 实现工具提示的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/272124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to implement tooltips for JTree?
提问by Touko
since JTree & TreeModel don't provide tooltips straight out-of-the-box, what do you think, what would be the best way to have item-specific tooltips for JTree?
由于 JTree 和 TreeModel 不直接提供开箱即用的工具提示,您认为,为 JTree 提供特定于项目的工具提示的最佳方法是什么?
Edit: (Answering my own question afterwards.)
编辑:(之后回答我自己的问题。)
@Zarkonnen: Thanks for the getTooltipText idea.
@Zarkonnen:感谢 getTooltipText 的想法。
I found out another (maybe still a bit nicer) way with overriding DefaultTreeCellRenderer and thought to share it:
我发现了另一种(可能仍然更好)覆盖 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);
}
}
采纳答案by Zarkonnen
See getTooltipTexton JTree. This should allow you to show tooltips depending on what in the tree is being hovered over. (Do read the docs though, you need to register the JTree with the ToolTipManager.)
请参阅JTree 上的getTooltipText。这应该允许您根据悬停在树中的内容来显示工具提示。(不过请阅读文档,您需要使用 ToolTipManager 注册 JTree。)
回答by Agustin
Yeah, you can use onMouseMovedand then use a method (I don't remember the name) that tells you in which node you are over. If you get null, obviously then you are not over a node.
是的,您可以使用onMouseMoved然后使用一种方法(我不记得名称)来告诉您在哪个节点结束。如果你得到空值,那么显然你还没有超过一个节点。
回答by Matthieu
When dealing with specific TreeNodesubclasses, based on your own answer and comments, I came up with an interface for my TreeNodeto implement.
在处理特定的TreeNode子类时,根据您自己的回答和评论,我想出了一个接口供我TreeNode实现。
Notice how we check if the valueis an intance of Tooltipablein the TreeCellRenderer:
请注意我们如何检查 是否value是Tooltipablein 的实例TreeCellRenderer:
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);
}

