File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem in adding node to JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in adding node to JTree" Watch "Problem in adding node to JTree" New topic

Problem in adding node to JTree

kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
I am dynamically adding leaf nodes to a JTree on click of a button. The node is added only if a parent node is selected first. How can I distinguish if the node selected is parent node or leaf node? If I use function isLeaf() for selected node, the function returns true even for parent node if there is no child node attached to it.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Is it possible that the node that you're calling isLeaf() on is the correct one?? I don't think it should return true always--that would be pretty useless.
However, to help you figure things out, you may want to try getChildCount() to make sure that it really is a leaf. My guess is that you are incorrectly calling these functions on the actual leaf instead of one of the parent nodes.

kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
The function isLeaf() returns true for a parent node that doesn't have any child yet added. I used function setAllowsChildren() to true for parent node but still facing same problem.
Purvi Pandya

Joined: Feb 11, 2004
Posts: 7
Below is example for DynamicTree. It might be helpful to u.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DynamicTree extends JFrame {
public DynamicTree(int n) {
super("Creating a Dynamic JTree");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
Container content = getContentPane();
JTree tree = new JTree(new OutlineNode(1, n));
content.add(new JScrollPane(tree), BorderLayout.CENTER);
setSize(300, 475);
public static void main(String[] args) {
int n = 5; // Number of children to give each node
if (args.length > 0)
try {
n = Integer.parseInt(args[0]);
} catch(NumberFormatException nfe) {
System.out.println("Can't parse number; using default of " + n);
new DynamicTree(n);
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class OutlineNode extends DefaultMutableTreeNode {
private boolean areChildrenDefined = false;
private int outlineNum;
private int numChildren;
public OutlineNode(int outlineNum, int numChildren) {
this.outlineNum = outlineNum;
this.numChildren = numChildren;
public boolean isLeaf() {
public int getChildCount() {
if (!areChildrenDefined)
private void defineChildNodes() {
// You must set the flag before defining children if you
// use "add" for the new children. Otherwise you get an infinite
// recursive loop, since add results in a call to getChildCount.
// However, you could use "insert" in such a case.
areChildrenDefined = true;
for(int i=0; i<numChildren; i++)
add(new OutlineNode(i+1, numChildren));
public String toString() {
TreeNode parent = getParent();
if (parent == null)
return(parent.toString() + "." + outlineNum);
Purvi Pandya
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
I found the solution to my problem by setting the following property.
Now the isLeaf() function returns false for parent nodes that can have child nodes
I agree. Here's the link:
subject: Problem in adding node to JTree
jQuery in Action, 3rd edition