问题描述
自JTree& TreeModel不提供开箱即用的工具提示,您认为,什么是JTree具有特定于项目的工具提示的最佳方式?
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?
(之后回答我自己的问题。)
(Answering my own question afterwards.)
@Zarkonnen:感谢getTooltipText的想法。
@Zarkonnen: Thanks for the getTooltipText idea.
我发现了另一种(可能还是更好的)覆盖DefaultTreeCellRenderer并且想要分享的方式:
I found out another (maybe still a bit nicer) way with overriding DefaultTreeCellRenderer and thought to share it:
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);
}
}
推荐答案
参见关于JTree。这应该允许您显示工具提示,具体取决于树中悬停的内容。 (但是,请阅读文档,您需要使用ToolTipManager注册JTree。)
See getTooltipText on 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工具提示的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!