Hi,

I need already written code for a simple file and folder explorer in Java similar to Project Explorer in Net Beans IDE. Instead of writing it from scratch I hope I can find which is already written. It will be very good if it allows file/directory add, remove and rename features. I searched for it on google but no success.

Thanks

Recommended Answers

All 6 Replies

So, waited until the last minute to do your project assignment, huh?

No this is not an assignment.

I searched for it on google but no success.
Thanks

Well if it is not on google you will have to write it yourself. If you have any trouble, post your code and you will get help.

Thanks for replying.

So here is the code which I have written so far

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testing;

/**
 *
 * @author Danish
 */
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;

public class SimpleTree extends JPanel {
  JTree tree;
  DefaultMutableTreeNode root;
  public SimpleTree() {
    root = new DefaultMutableTreeNode("root", true);
    getList(root, new File("c:/Program Files"));
    setLayout(new BorderLayout());
    tree = new JTree(root);
    tree.setRootVisible(false);
    add(new JScrollPane((JTree)tree),"Center");
    }

  public Dimension getPreferredSize(){
    return new Dimension(200, 120);
    }

  public void getList(DefaultMutableTreeNode node, File f) {
     if(!f.isDirectory()) {
         // We keep only JAVA source file for display in this HowTo
         if (f.getName().endsWith("java")) {
            System.out.println("FILE  -  " + f.getName());
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
            node.add(child);
            }
         }
     else {
         System.out.println("DIRECTORY  -  " + f.getName());
         DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
         node.add(child);
         File fList[] = f.listFiles();
         for(int i = 0; i  < fList.length; i++)
             getList(child, fList[i]);
         }
    }
  
  public static void main(String s[]){
    MyJFrame frame = new MyJFrame("Directory explorer");
    }
  }

class WindowCloser extends WindowAdapter {
  public void windowClosing(WindowEvent e) {
    Window win = e.getWindow();
    win.setVisible(false);
    System.exit(0);
    }
  }

class MyJFrame extends JFrame {
  JButton b1, b2, b3;
  SimpleTree panel;
  MyJFrame(String s) {
    super(s);
    panel = new SimpleTree();
    getContentPane().add(panel,"Center");
    setSize(300,300);
    setVisible(true);
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowCloser());
    }

}

As you can see from the code that I specify "C:/Program Files" folder as a root folder to this tree. I want "My Computer" to be the top level root node.

Thanks

I don't know if it is possible to get the "My Computer" as root, but check this out:

File [] files = File.listRoots();
        for (int i=0;i<files.length;i++) {
            System.out.println(files[i]);
        }

This will get you all the files that are under the "My Computer". So instead of having one root ("C:/program files"),
you can create one father, and under that father put as nodes the "Files" that the above code returns:
C://, E://, D://

I don't know if it makes sense, but you can have this:

root = new DefaultMutableTreeNode("root", true);
 
          File fList[] = File.listRoots();
          for(int i = 0; i  < fList.length; i++)
              getList(root , fList[i]);
          }

After all, under "My Computer", all you have are the drives of your computer: C:/, D:/

Also you can check this tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

And I would suggest not to populate the tree recursively because it takes awfully long. Try to put Listeners and when a folder is clicked, then take and add the children.

Question: Have you tried JFileChooser?

Thankyou very much for your response. This solves the problem.

In this perticular situation I do not need JFileChooser. I need something similar to "Project Explorer" in Net Beans IDE.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.