Saturday, 11 February 2012


Want to have a very basic JTree example, then you are at the right place.

Example 1
Code:

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class SimpleJTree
{
    public static void main(String[] args) {
      
        JFrame frame = new JFrame();

        DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Color", true);
        DefaultMutableTreeNode black = new DefaultMutableTreeNode("Black");
        DefaultMutableTreeNode blue = new DefaultMutableTreeNode("Blue");
        DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("Navy Blue");
        DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("Dark Blue");
        DefaultMutableTreeNode green = new DefaultMutableTreeNode("Green");
        DefaultMutableTreeNode white = new DefaultMutableTreeNode("White");

        DefaultMutableTreeNode adsf=new DefaultMutableTreeNode();

        parent.add(black);
        parent.add(blue);

        blue.add(nBlue);
        blue.add(dBlue);

        parent.add(green);
        parent.add(white);

        JTree tree = new JTree(parent);
        frame.add(tree);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}


Download : SimpleJTree.java

Description: 
The program is self explanatory in itself. One can use a IDE say Netbeans to explore the full feature of various methods and constructors used in the program. Here the Tree is hard-coded, required to make up the basics.




Example 2
Code:

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class AdvancedJTree
{
    public static void main(String[] args)
    {
        JFileChooser fchoose=new JFileChooser();
        fchoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fchoose.showOpenDialog(fchoose);

        File f=fchoose.getSelectedFile();
        //File f=new File(".");
//        System.out.println(f.getParentFile());
      
//        JTree tr=new JTree(createTree(f.getParentFile()));
        JTree tr=new JTree(createTree(f));
        JScrollPane scp=new JScrollPane(tr);

        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.add(scp);
        frame.setVisible(true);

    }
  
    public static DefaultMutableTreeNode createTree(File fil)
    {
        DefaultMutableTreeNode root=new DefaultMutableTreeNode(fil.getName(), true);

        String []list;
        DefaultMutableTreeNode temp;

        if(fil.isDirectory())
        {
            list=fil.list();
            if(list==null){ return root;    }
          
//            System.out.println("Files in Directory");
            for(String s:list){
                System.out.println(s);
            }

//            System.out.println("Files in Directory End");
          
            int index=0;

            while(index<list.length)
            {
                File tempFile=new File(fil.getPath()+fil.separatorChar+list[index]);

                if(tempFile.isFile()){
                    temp=new DefaultMutableTreeNode(tempFile.getName());
                    root.add(temp);
                }
                else {
                    temp=createTree(tempFile);
                    root.add(temp);
                }
                index++;
            }
        }
        return root;
    }
}


Download : AdvancedJTree.java

Description:
This is an advanced version of the previous program. Here the Tree is built dynamically by selecting any folder of your choice at the time of startup of this program. Since at the startup an Open Dialog is shown, to select a particular folder whose Tree will be built dynamically. The Tree is built recursively. The technique used is such that, traverse every file in a folder and add it to Tree, but if the file is itself a folder, call the function recursively.

3 comments:

  1. 1)I am added system file with jtree and checkbox format view like C,D,E,G drives into the Jfile chooser.
    2)when I am click on checkbox there will be display the size of their
    Corresponding folder and also selected folder name can be shows in folder name of file chooser dialog box.

    Can u help me pls Urgentttttttttttt?

    ReplyDelete
    Replies
    1. haiiiiiiiiiiiii GM,

      Help me its urgent

      Delete
  2. How to configure jtree as a jfilechooser in swings ?

    ReplyDelete