Jtree:将backgroundColor设置为最大单击双击节点

时间:2011-11-14 03:05:29

标签: java swing jtree

双击我高亮显示双击的Jtree节点的背景为绿色。当我双击其他节点时,先前选择的绿色节点应恢复为原始节点,新双击的节点应亮起绿色。

它有点工作但行为不一致。当我双击子节点时,它们将变为绿色,之前双击的节点保持绿色,直到我选择该节点并选择另一个节点....

这是我的自定义渲染器。

 public class MyRenderer extends DefaultTreeCellRenderer{


        public MyRenderer() {
        }

    public Component getTreeCellRendererComponent(
                        JTree tree,
                        Object value,
                        boolean sel,
                        boolean expanded,
                        boolean leaf,
                        int row,
                        boolean hasFocus) {

        super.getTreeCellRendererComponent(
                        tree, value, sel,
                        expanded, leaf, row,
                        hasFocus);

        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        NodePro np = (NodePro)(node.getUserObject());

        if(np.getNodeancestors() != null){
            if(np.getNodeancestors().contains("activated")){
                setBackgroundNonSelectionColor(Color.GREEN);

            }else{
                setBackgroundNonSelectionColor(null);

            }
        }else{
            setBackgroundNonSelectionColor(null);

        }

        return this;
    }


}

这是我的Jtree,它添加了双击监听器。

  public class Tree extends JTree{
        private static Tree INSTANCE;
        private TreeSelectionListenerClass tsl;
        private TreePopupTriggerListener ptl;
        private TreeDoubleClickListener dbll;

        public Tree() {
            // TODO Auto-generated constructor stub

                tsl = new TreeSelectionListenerClass();
                dbll = new TreeDoubleClickListener();

        addTreeSelectionListener(tsl);
        addMouseListener(dbll);
        setToggleClickCount(0);
        setCellRenderer(new MyRenderer());

    }

这是我抓住双击的地方....

public class TreeDoubleClickListener implements MouseInputListener {

    public TreeDoubleClickListener(){

    }

    /*On Double Click (This Node Will Be Activated), Save To Activated Node */
    @Override
    public void mouseClicked(MouseEvent e) {

        if (e.getClickCount() == 2){            
            Global.showWaitCursor();

            try{
                DefaultMutableTreeNode selected = (DefaultMutableTreeNode) Global.previousTreePath.getLastPathComponent();
                NodePro npx = (NodePro) selected.getUserObject();
                npx.setNodeancestors("");

                 TreePath path = Tree.getInstance().getPathForLocation(e.getX(), e.getY());
                  if (path != null){                  
                      Global.previousTreePath = path;
                      DefaultMutableTreeNode current = (DefaultMutableTreeNode) Global.previousTreePath.getLastPathComponent();
                      NodePro npt = (NodePro) current.getUserObject();
                      npt.setNodeancestors("activated");
                  }

            }catch(Exception zxcv){
                //save this path to edit later
                  TreePath path = Tree.getInstance().getPathForLocation(e.getX(), e.getY());
                  if (path != null){                  
                      Global.previousTreePath = path;
                      DefaultMutableTreeNode current = (DefaultMutableTreeNode) Global.previousTreePath.getLastPathComponent();
                      NodePro npt = (NodePro) current.getUserObject();
                      npt.setNodeancestors("activated");
                  }
            }


}

}

1 个答案:

答案 0 :(得分:2)

除了更改节点的状态外,您还必须通知模型有关更改的信息。假设模型的类型为DefaultTreeModel(使用自定义模型,则需要通过相应的方法发送消息)

 // change node/user object
 DefaultMutableTreeNode current = (DefaultMutableTreeNode) 
          Global.previousTreePath.getLastPathComponent();
 NodePro npt = (NodePro) current.getUserObject();
 npt.setNodeancestors("activated");
 // notify model
 JTree tree = (JTree) e.getSource();
 ((DefaultTreeModel) tree.getModel()).nodeChanged(current);