通过引用操纵jtree节点似乎不起作用(摆动) [英] manipulate jtree node via references seems not working (swing)

查看:73
本文介绍了通过引用操纵jtree节点似乎不起作用(摆动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题(这与我的帖子

I have the following problem (this is related to my post blink a tree node):
I have a custom cell renderer.
In some part of my code I create a new DefaultMutableTreeNode and store it in a list

public static List<DefaultMutableTreeNode> nodes = new ArrayList<DefaultMutableTreeNode>() 
//in some time
DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("SomeValue");
nodes.add(node);

在单元格渲染器中,我这样做:

In my cell renderer I do:

public Component getTreeCellRendererComponent(JTree tree, Object value,
      boolean selected, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {
    DefaultMutableTreeNode n = (DefaultMutableTreeNode)value;
    if(nodes.contains(n)){
     //set background to red
    }
}

这时节点有一个节点,但是代码永远不会进入if分支.
为什么?我不明白,因为我已经将其存储在arraylist中.我会得到其他参考吗?
我还创建了一个摇摆计时器:

At this point nodes has a node but the code never goes in the if branch.
Why? I can not understand since I already stored it in the arraylist. Do I get a different reference?
Also I created a swing timer:

Timer t = new Timer(400, new ActionListener(){
   public void actionPerformed(ActionEvent evt) {
        if(nodes.size == 0)
            return;
        TreePath p = new TreePath(nodes.get(0));
        Rectangle r = tree.getPathBounds(p);
        tree.repaint(r);
   }

});    

但是我在tree.getPathBounds中得到了NPE.
我不明白为什么.我不能以这种方式操作存储在列表中的DefaultMutableNodes吗?我在想什么错?
注意:如果我只是简单地调用repaint();在计时器和单元格渲染器中,我遍历节点以查看它是否显示与我存储的节点相同的文本,我希望我闪烁的内容有效

But I get a NPE in tree.getPathBounds.
I can not understand why. Can't I manipulate DefaultMutableNodes I stored in my list this way? What am I doing wrong in my thinking?
Note: If I simply call repaint(); in the timer and in the cell renderer I loop over the nodes to see if it displays the sametext with the node I have stored, what I want I get the blinking, works

谢谢

推荐答案

实际上TreePath是对象列表...从树根到节点的路径.如果您从单个节点创建路径,则仅当该节点是树的根节点时,该路径才存在于树中. 我建议使用TreeSelectionEvent public TreePath[] getPaths()方法.该方法提供了实际路径.

Actually TreePath is a list of objects... path from tree root to the node. If you create a path from the single node the path exists in the tree only if the node is root of the tree. I woul recommend to use TreeSelectionEvent public TreePath[] getPaths() method. The method provides actual paths.

这篇关于通过引用操纵jtree节点似乎不起作用(摆动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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