简体   繁体   English

Jtree仅选择子节点而不选择父节点

[英]Jtree Select only Child Node and not Parent

I am using a Jtree where am stuck at a place to do the below, 我正在使用Jtree,它被卡在某个地方以进行以下操作,

图

From Fig:2 I click on A(Check box) and all its child is getting selected - It's fine 从图2中, I click on A(Check box) and all its child is getting selected - It's fine

From Fig:3 I Clicked D(CheckBox) and all its parents (A,B,C) are also geting selected. 从图3中, I Clicked D(CheckBox) and all its parents (A,B,C) are also geting selected.

Now what i want to do is, 现在我要做的是

If i select A -> A,B,C,D will eb selected
If i Select B-> only B,C,D
If c -> C,D
If D-> Only D

The Code i use is: 我使用的代码是:

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

                //delegate is TreeCellrenderer
                Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); 
                TreePath path = tree.getPathForRow(row);
                if(path!=null)
                {         
                    //Selection Model is TreeSelection Model
                    if(selectionModel.isPathSelected(path, true)) //Below code.
                    {       
                        checkBox.setState(checkBox.SELECTED);   
                        if (selected) 
                        {
                            //Comes here If check box is selected by mouse click
                        }
                    }
                    else
                    {                       
                        checkBox.setState(checkBox.NOT_SELECTED);   
                    }
                } 
                add(checkBox, BorderLayout.WEST); 
                add(renderer, BorderLayout.CENTER);  
                return this; 
            }

The isPathSelected Method: isPathSelected方法:

// tells whether given path is selected. 
        // if dig is true, then a path is assumed to be selected, if 
        // one of its ancestor is selected. 
        public boolean isPathSelected(TreePath path, boolean dig){                      
            if(!dig){
                return super.isPathSelected(path); 
            }

            while(path!=null && !super.isPathSelected(path)){
                path = path.getParentPath(); 
            }

            return path!=null; 
        } 

Am not sure whether am doing it roght am new to Jtree. 不知道是否对Jtree来说是新的。 Please help 请帮忙

you must 'walk through' the child nodes from the tree element: 您必须从树元素“遍历”子节点:

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    walkThroug(node, isSelected);
}

private void walkThrough(DefaultMutableTreeNode node, boolean isSelected) {
    int cc = node.getChildCount();

    //TODO select current node
    //something like: node.setSelected(isSelected)

    for(int i = 0; i < cc; i++){
        DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
        walkThrough(childNode, isSelected);
    }
}

this is a recursive call, and it's only a snippet, but it should work this way... maybe it needs some other parameters on the 'walkThrough'-methode... 这是一个递归调用,它只是一个片段,但是它应该以这种方式工作……也许在“ walkThrough”方法上需要其他一些参数……

i'm asserting that your object (value) is a DefaultMutableTreeNode as described on http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html 我断言您的对象(值)是DefaultMutableTreeNode,如http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM