我希望我的jtree有root
parent
和child
。
用户不应该添加孩子
我该怎么做?
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
// i need to add some code here to restrict adding if element is a child
// if ( the selected element is a child ) then the following code shall not work
if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode) (parentPath
.getLastPathComponent());
}
return addObject(parentNode, child, true);
}