问题描述
我有一个填充了一些(自定义)节点的JTree.我有一个类作为DefaultTreeCellRenderer的子类,并使用该"MyTreeCellRenderer"可以为树中的每个节点设置工具提示.它的工作原理很好:填充了JTree,设置了单元格渲染器,所有添加的节点都具有工具提示.问题是我不知道如何为已经填充的树中的某些节点更改工具提示...该怎么做?有什么方法可以仅针对JTree中的一个节点重新创建"单元格渲染器吗?
I have a JTree populated with some (custom) nodes. I have a class that's subclass of DefaultTreeCellRenderer and using that "MyTreeCellRenderer" i can set tooltips for each node in my tree. It works fine: JTree is populated, cell renderer is set, all added nodes do have tooltips.Problem is that i don't know how to change tooltip for certain node in already populated tree... How to do that? Is there any way to "recreate" cell renderer for just one node in JTree?
推荐答案
我做到了!我没有使用扩展的CellRenderer,而是使用树的"getTooltipText"方法(我扩展了树).这样,我就可以根据将鼠标指针悬停在其上方的对象来控制工具提示的文本.
I did it! Instead of using extended CellRenderer i used "getTooltipText" method of tree (i extended the tree). That way i was able to control text of tooltip according to object that has mouse pointer over it.
@Override
public String getToolTipText(MouseEvent evt) {
if (getRowForLocation(evt.getX(), evt.getY()) == -1)
return null;
TreePath curPath = getPathForLocation(evt.getX(), evt.getY());
TreeNode node = (TreeNode)curPath.getLastPathComponent();
if(something)
return "Empty";
if(something_else)
return "Not empty";
return null;
}
在呈现工具提示之前,您还需要告知工具提示管理器您的树:
You will also need to tell the tooltip manager about your tree before the tooltips will render:
ToolTipManager.sharedInstance().registerComponent(myTree);
这篇关于JTree节点的可更改工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!