943,928 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3168
  • Java RSS
Sep 4th, 2009
0

File and Folder Explorer code in Java

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
dasatti is offline Offline
57 posts
since Dec 2008
Sep 4th, 2009
0

Re: File and Folder Explorer code in Java

So, waited until the last minute to do your project assignment, huh?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 4th, 2009
0

Re: File and Folder Explorer code in Java

No this is not an assignment.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
dasatti is offline Offline
57 posts
since Dec 2008
Sep 4th, 2009
0

Re: File and Folder Explorer code in Java

Click to Expand / Collapse  Quote originally posted by dasatti ...
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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Sep 4th, 2009
0

Re: File and Folder Explorer code in Java

Thanks for replying.

So here is the code which I have written so far

Java Syntax (Toggle Plain Text)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package testing;
  7.  
  8. /**
  9.  *
  10.  * @author Danish
  11.  */
  12. import javax.swing.*;
  13. import javax.swing.tree.*;
  14. import java.awt.event.*;
  15. import java.awt.*;
  16. import java.util.*;
  17. import java.io.*;
  18.  
  19. public class SimpleTree extends JPanel {
  20. JTree tree;
  21. DefaultMutableTreeNode root;
  22. public SimpleTree() {
  23. root = new DefaultMutableTreeNode("root", true);
  24. getList(root, new File("c:/Program Files"));
  25. setLayout(new BorderLayout());
  26. tree = new JTree(root);
  27. tree.setRootVisible(false);
  28. add(new JScrollPane((JTree)tree),"Center");
  29. }
  30.  
  31. public Dimension getPreferredSize(){
  32. return new Dimension(200, 120);
  33. }
  34.  
  35. public void getList(DefaultMutableTreeNode node, File f) {
  36. if(!f.isDirectory()) {
  37. // We keep only JAVA source file for display in this HowTo
  38. if (f.getName().endsWith("java")) {
  39. System.out.println("FILE - " + f.getName());
  40. DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
  41. node.add(child);
  42. }
  43. }
  44. else {
  45. System.out.println("DIRECTORY - " + f.getName());
  46. DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
  47. node.add(child);
  48. File fList[] = f.listFiles();
  49. for(int i = 0; i < fList.length; i++)
  50. getList(child, fList[i]);
  51. }
  52. }
  53.  
  54. public static void main(String s[]){
  55. MyJFrame frame = new MyJFrame("Directory explorer");
  56. }
  57. }
  58.  
  59. class WindowCloser extends WindowAdapter {
  60. public void windowClosing(WindowEvent e) {
  61. Window win = e.getWindow();
  62. win.setVisible(false);
  63. System.exit(0);
  64. }
  65. }
  66.  
  67. class MyJFrame extends JFrame {
  68. JButton b1, b2, b3;
  69. SimpleTree panel;
  70. MyJFrame(String s) {
  71. super(s);
  72. panel = new SimpleTree();
  73. getContentPane().add(panel,"Center");
  74. setSize(300,300);
  75. setVisible(true);
  76. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  77. addWindowListener(new WindowCloser());
  78. }
  79.  
  80. }

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
dasatti is offline Offline
57 posts
since Dec 2008
Sep 4th, 2009
1

Re: File and Folder Explorer code in Java

I don't know if it is possible to get the "My Computer" as root, but check this out:
Java Syntax (Toggle Plain Text)
  1. File [] files = File.listRoots();
  2. for (int i=0;i<files.length;i++) {
  3. System.out.println(files[i]);
  4. }

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)
  1. root = new DefaultMutableTreeNode("root", true);
  2.  
  3. File fList[] = File.listRoots();
  4. for(int i = 0; i < fList.length; i++)
  5. getList(root , fList[i]);
  6. }

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?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Sep 4th, 2009
0

Re: File and Folder Explorer code in Java

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
dasatti is offline Offline
57 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help with Bulls and Cows game
Next Thread in Java Forum Timeline: Searching code help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC