Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2007

Tip: Looking for answers? Try searching our database.

Allow multiple selection only for leaves of a JTree

Thread view: 
Chanchal - 18 Dec 2007 12:43 GMT
Hello All,

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

Chanchal
Alexander.V.Kasatkin@gmail.com - 21 Dec 2007 22:41 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.