| | |
File and Folder Explorer code in Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 47
Reputation:
Solved Threads: 3
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
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
maSteR of aLL, jAck oF NoNe
So, waited until the last minute to do your project assignment, huh?
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Dec 2008
Posts: 47
Reputation:
Solved Threads: 3
Thanks for replying.
So here is the code which I have written so far
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
So here is the code which I have written so far
Java Syntax (Toggle Plain Text)
/* * 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
Last edited by dasatti; Sep 4th, 2009 at 4:42 am.
maSteR of aLL, jAck oF NoNe
I don't know if it is possible to get the "My Computer" as root, but check this out:
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:
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/tutor...ents/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?
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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/tutor...ents/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?
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- create a file in the folder with username & password (VB.NET)
- Upload all the file in a folder to Server using JavaScript / web service (JavaScript / DHTML / AJAX)
- Open csv file, run code, save, quit, repeat 3000ish times. (Visual Basic 4 / 5 / 6)
- How to link to a file in other folder? (ASP.NET)
- create, write & read file to/from folder (C++)
- Error deleting file or folder (Viruses, Spyware and other Nasties)
- Error deleting file or folder (Windows NT / 2000 / XP)
- IE, File Explorer and File Folder Short Cuts will not open (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: help with Bulls and Cows game
- Next Thread: Searching code help
Views: 547 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation awt binary bluetooth busy_handler(null) card chat class classes client code collision component constructor database detection draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop machine map method methods mobile netbeans newbie nls number object oracle os parsing plazmic print problem program programming project recursion remote scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test threads time transfer tree unlimited webservices windows






