使用treeNode本身在Jtree中突出显示treeNode [英] highlight the treeNode in the Jtree using the treeNode itself

查看:98
本文介绍了使用treeNode本身在Jtree中突出显示treeNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTree( myTree ),在另一堂课中,我有一个DefaultMutableTreeNode,它取自 myTree .

I have a JTree (myTree) and in another class, I have a DefaultMutableTreeNode which was taken from myTree.

在某些功能中,我希望JTree突出显示该节点.

In a certain function, I want the JTree to highlight the node.

我尝试过:

I tried:

myTree.setSelectionPath(new TreePath(treeNode));

但是在视觉上什么也没发生.

but visually nothing is happening.

有什么想法吗?

更新:

我还有另一个JTable,它是根据 myTree 中选择的treeNode呈现的.该表正在正确更新.只是 myTree 拒绝以视觉方式进行更新.

I have also another JTable which is rendered based on the selected treeNode in myTree. The table is updating correctly. It's just the myTree which refused to update visually.

推荐答案

您需要使用节点的实际树路径.不只是TreePath的实例:

You need to use the actual tree path of the node. Not just an instance of TreePath:

myTree.setSelectionPath(new TreePath(treeModel.getPathToRoot(treeNode)));

此外,javadoc说:

Also, the javadoc says:

如果路径的任何部分都被隐藏(在折叠的节点下),并且getExpandsSelectedPaths为true,则它将被暴露(使其可见)

If any component of the path is hidden (under a collapsed node), and getExpandsSelectedPaths is true it is exposed (made viewable)

因此请确保getExpandsSelectedPaths为真.

这篇关于使用treeNode本身在Jtree中突出显示treeNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆