Re: Allow multiple selection only for leaves of a JTree
- From: Alexander.V.Kasatkin@xxxxxxxxx
- Date: Fri, 21 Dec 2007 14:41:00 -0800 (PST)
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
.
- References:
- Allow multiple selection only for leaves of a JTree
- From: Chanchal
- Allow multiple selection only for leaves of a JTree
- Prev by Date: Re: Swing Gui / Ant
- Next by Date: Re: Swing Gui / Ant
- Previous by thread: Allow multiple selection only for leaves of a JTree
- Next by thread: JSplitpane divider won't go up !!
- Index(es):