How can I load a Jtree directory with children on expanding the node of that directory.
I have developed the following SSCCE. Please explain in its context.
My specific question is, when you execute the code given here, you can see the file bb.1 inside the directory bb.
bb.1 is a dummy which I used while creating the Jtree. When I expand bb, instead of the leaf bb.1 , I want to display some other file name, say qq.1 or jj.1.
What should I do?
Hope my question is clear.
Java Code:
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class FileTreeFrame extends JFrame {
  private JTree fileTree;
  private FileSystemModel fileSystemModel;
  public FileTreeFrame(String abc) {
    super("JTree FileSystem Viewer");
    // Build up your data
    List rootChildren = new ArrayList();
    rootChildren.add( new MyNode("aa") );
    List bbChildren = new ArrayList();
    bbChildren.add( new MyNode("bb.1") );
    rootChildren.add( new MyNode("bb", bbChildren) );
    rootChildren.add( new MyNode("cc") );
    MyNode rootNode = new MyNode("root", rootChildren);
    fileTree = new JTree(new FileSystemModel(new MyTreeNode(rootNode)));
    setSize(640, 480);
  public static void main(String args[]) {
    new FileTreeFrame("");

class FileSystemModel extends DefaultTreeModel {
    public FileSystemModel(DefaultMutableTreeNode node) {
    public boolean isLeaf(Object node) {
          MyTreeNode treeNode = (MyTreeNode)node;
          return !((MyNode)treeNode.getUserObject()).hasChildren();
import java.util.List;

public class MyNode {
      private String name;
      private List children;
      public MyNode(String name) {
            this.name = name;
      public MyNode(String name, List children) {
            this.name = name;
            this.children = children;

      public boolean hasChildren() {
            return children!=null && children.size()>0;
      public String toString() {
            return name;
      public List getChildren() {
            return children;


import java.util.Iterator;

import javax.swing.tree.DefaultMutableTreeNode;

public class MyTreeNode extends DefaultMutableTreeNode {

    public MyTreeNode(MyNode node) {

    private void addSubNodes() {
          MyNode content = (MyNode)getUserObject();
          if (content!=null && content.hasChildren()) {
                for (Iterator it = content.getChildren().iterator(); it.hasNext();) {
                      add( new MyTreeNode((MyNode)it.next()) );
Please help with a solution.