Hi Chanchal,
> How can i enforce that multilpe selection is allowed only for the the
> leaves of a JTree and not for other nodes. Kindly advice. Thanks in
> advance
The simplest solution is to enhance the DefaultTreeSelectionModel
class (Java 6 listing below):
public class RestrictedTreeSelectionModel extends
DefaultTreeSelectionModel {
private static final long serialVersionUID =
-4055323556188518616L;
@Override
public void setSelectionPaths(final TreePath[] pPaths) {
final ArrayList<TreePath> temp = new ArrayList<TreePath>();
for (int i = 0, n = pPaths != null ? pPaths.length : 0; i < n;
i++) {
final Object lastPathComponent =
pPaths[i].getLastPathComponent();
if (lastPathComponent instanceof TreeNode) {
if (((TreeNode) lastPathComponent).isLeaf()) {
temp.add(pPaths[i]);
}
}
}
if (!temp.isEmpty()) {
super.setSelectionPaths(temp.toArray(new
TreePath[temp.size()]));
}
}
}
This solution has one negative effect:
if the selection mode is CONTIGUOUS_TREE_SELECTION,
only children of the same parent can be selected.
BR, Alex