I created this method to return the index of a leaf node from its leaf nodes in the JTree object. Using the TreePaths of the root and the leaf node to search for, the method loops through all the root’s contents until it encounters the leaf node equal to the one passed to the second parameter.

I made a method for a JTree object that will mimic paging features within its leaf nodes. If you have FIRST, NEXT, PREVIOUS and LAST buttons and you click say, NEXT, it will go to the next leaf node of the JTree. If the path to that leaf node is not expanded, it will expand it right away. This is useful in cases wherein you only want the navigation to affect leaf nodes.

The method has 2 parameters: first is the JTree object while the second will require you what kind of paging you want. Either the FIRST, NEXT, PREVIOUS and LAST leaf nodes of the JTree. Here is the code:

To call the method to go to the next leaf node available, you can do it like this:

Use this helper method that I created to expand the first leaf node of a JTree object. By default, only the child nodes of the root are displayed. Some cases, you may want to display the very first leaf node of a JTree object.

This method assumes that the first child node of the root has a child of its own. This method also does not care how deep the TreePath is. It will expand all nodes of the TreePath until the leaf node.

Related Posts Plugin for WordPress, Blogger...