문제

So I want to be able to only select leafs in JTree. There are some solutions online, but they don't work on multiple selection...

What I would like is to find the part of the code that fires when user clicks on a node and modify that part to suit my needs.

I have found a listener within DefaultTreeCellEditor, but that code seem to apply to when only one node is selected at a time...

The bottom line is, where can I find the code that, when nodes gets clicked, decides if it will select it or not, and will it or not deselect all the other selected nodes?

도움이 되었습니까?

해결책

Fixed it!

public class LeafOnlyTreeSelectionModel extends DefaultTreeSelectionModel
{
private static final long serialVersionUID = 1L;

private TreePath[] augmentPaths(TreePath[] pPaths)
{
    ArrayList<TreePath> paths = new ArrayList<TreePath>();

    for (int i = 0; i < pPaths.length; i++)
    {
        if (((DefaultMutableTreeNode) pPaths[i].getLastPathComponent()).isLeaf())
        {
            paths.add(pPaths[i]);
        }
    }

    return paths.toArray(pPaths);
}

@Override
public void setSelectionPaths(TreePath[] pPaths)
{
    super.setSelectionPaths(augmentPaths(pPaths));
}

@Override
public void addSelectionPaths(TreePath[] pPaths)
{
    super.addSelectionPaths(augmentPaths(pPaths));
}

}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top