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 / General / November 2005

Tip: Looking for answers? Try searching our database.

JTree navigation

Thread view: 
soup_or_power@yahoo.com - 21 Nov 2005 16:59 GMT
Here are some useful tips to navigate the tree. Can anyone tell me the
limitations of getNextMatch.

Thank you

// Create tree
   JTree tree = new JTree();

   // Search forward from first visible row looking for any visible
node
   // whose name starts with prefix.
   int startRow = 0;
   String prefix = "b";
   TreePath path = tree.getNextMatch(prefix, startRow,
Position.Bias.Forward);

   // Search backward from last visible row looking for any visible
node
   // whose name starts with prefix.
   startRow = tree.getRowCount()-1;
   prefix = "b";
   path = tree.getNextMatch(prefix, startRow, Position.Bias.Backward);

   // Find the path (regardless of visibility) that matches the
   // specified sequence of names
   path = findByName(tree, new String[]{"JTree", "food", "bananas"});

   // Finds the path in tree as specified by the node array. The node
array is a sequence
   // of nodes where nodes[0] is the root and nodes[i] is a child of
nodes[i-1].
   // Comparison is done using Object.equals(). Returns null if not
found.
   public TreePath find(JTree tree, Object[] nodes) {
       TreeNode root = (TreeNode)tree.getModel().getRoot();
       return find2(tree, new TreePath(root), nodes, 0, false);
   }

   // Finds the path in tree as specified by the array of names. The
names array is a
   // sequence of names where names[0] is the root and names[i] is a
child of names[i-1].
   // Comparison is done using String.equals(). Returns null if not
found.
   public TreePath findByName(JTree tree, String[] names) {
       TreeNode root = (TreeNode)tree.getModel().getRoot();
       return find2(tree, new TreePath(root), names, 0, true);
   }
   private TreePath find2(JTree tree, TreePath parent, Object[] nodes,
int depth, boolean byName) {
       TreeNode node = (TreeNode)parent.getLastPathComponent();
       Object o = node;

       // If by name, convert node to a string
       if (byName) {
           o = o.toString();
       }

       // If equal, go down the branch
       if (o.equals(nodes[depth])) {
           // If at end, return match
           if (depth == nodes.length-1) {
               return parent;
           }

           // Traverse children
           if (node.getChildCount() >= 0) {
               for (Enumeration e=node.children();
e.hasMoreElements(); ) {
                   TreeNode n = (TreeNode)e.nextElement();
                   TreePath path = parent.pathByAddingChild(n);
                   TreePath result = find2(tree, path, nodes, depth+1,
byName);
                   // Found a match
                   if (result != null) {
                       return result;
                   }
               }
           }
       }
   
       // No match at this branch
       return null;
   }
Dave Glasser - 21 Nov 2005 22:56 GMT
You forgot to include this part:

© 2002 Addison-Wesley.

http://javaalmanac.com/egs/javax.swing.tree/FindNode.html

"soup_or_power@yahoo.com" <soup_or_power@yahoo.com> wrote on 21 Nov
2005 08:59:10 -0800 in comp.lang.java.programmer:

>Here are some useful tips to navigate the tree. Can anyone tell me the
>limitations of getNextMatch.
[quoted text clipped - 80 lines]
>        return null;
>    }


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.