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: dasatti is an unknown quantity at this point 
Solved Threads: 3
dasatti dasatti is offline Offline
Light Poster

File and Folder Explorer code in Java

 
0
  #1
Sep 4th, 2009
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
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: File and Folder Explorer code in Java

 
0
  #2
Sep 4th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 47
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 3
dasatti dasatti is offline Offline
Light Poster

Re: File and Folder Explorer code in Java

 
0
  #3
Sep 4th, 2009
No this is not an assignment.
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: File and Folder Explorer code in Java

 
0
  #4
Sep 4th, 2009
Originally Posted by dasatti View Post
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 47
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 3
dasatti dasatti is offline Offline
Light Poster

Re: File and Folder Explorer code in Java

 
0
  #5
Sep 4th, 2009
Thanks for replying.

So here is the code which I have written so far

  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.
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: File and Folder Explorer code in Java

 
1
  #6
Sep 4th, 2009
I don't know if it is possible to get the "My Computer" as root, but check this out:
  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:
  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?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 47
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 3
dasatti dasatti is offline Offline
Light Poster

Re: File and Folder Explorer code in Java

 
0
  #7
Sep 4th, 2009
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.
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 547 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC