Folder Explorer in Java with Source Code

Folder Explorer in Java with Source Code

Folder Explorer in Java with Source Code: We can develop Folder Explorer in java with the help of IO Stream, AWT/Swing with event handling. Let’s see the code of creating Folder Explorer in java.

  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.tree.*;
  6. /***********************************/
  7. class Explorer extends JPanel implements ActionListener
  8. {
  9. JTextField jtf;
  10. JTextArea jta;
  11. JTree tree;
  12. JButton refresh;
  13. JTable jtb;
  14. JScrollPane jsp;
  15. JScrollPane jspTable;
  16. String currDirectory=null;
  17. final String[] colHeads={“File Name”,“SIZE(in Bytes)”,“Read Only”,“Hidden”};
  18. String[][]data={{“”,“”,“”,“”,“”}};
  19. /////////////////////////////////
  20. Explorer(String path)
  21. {
  22. jtf=new JTextField();
  23. jta=new JTextArea(5,30);
  24. refresh=new JButton(“Refresh”);
  25. File temp=new File(path);
  26. DefaultMutableTreeNode top=createTree(temp);
  27. //if(top!=null)
  28. tree=new JTree(top);
  29. jsp=new JScrollPane(tree);
  30. final String[] colHeads={“File Name”,“SIZE(in Bytes)”,“Read Only”,“Hidden”};
  31. String[][]data={{“”,“”,“”,“”,“”}};
  32. jtb=new JTable(data, colHeads);
  33. jspTable=new JScrollPane(jtb);
  34. setLayout(new BorderLayout());
  35. add(jtf,BorderLayout.NORTH);
  36. add(jsp,BorderLayout.WEST);
  37. add(jspTable,BorderLayout.CENTER);
  38. add(refresh,BorderLayout.SOUTH);
  39. tree.addMouseListener(
  40. new MouseAdapter()
  41. {
  42. public void mouseClicked(MouseEvent me)
  43. {
  44. doMouseClicked(me);
  45. }
  46. });
  47. jtf.addActionListener(this);
  48. refresh.addActionListener(this);
  49. }
  50. ///////////////////////////////
  51. public void actionPerformed(ActionEvent ev)
  52. {
  53. File temp=new File(jtf.getText());
  54. DefaultMutableTreeNode newtop=createTree(temp);
  55. if(newtop!=null)
  56.     tree=new JTree(newtop);
  57. remove(jsp);
  58. jsp=new JScrollPane(tree);
  59. setVisible(false);
  60. add(jsp,BorderLayout.WEST);
  61. tree.addMouseListener(
  62. new MouseAdapter()
  63. {
  64. public void mouseClicked(MouseEvent me)
  65. {
  66. doMouseClicked(me);
  67. }
  68. });
  69. setVisible(true);
  70. }
  71. //////////////////////////////
  72. DefaultMutableTreeNode createTree(File temp)
  73. {
  74. DefaultMutableTreeNode top=new DefaultMutableTreeNode(temp.getPath());
  75. if(!(temp.exists() && temp.isDirectory()))
  76.     return top;
  77. fillTree(top,temp.getPath());
  78. return top;
  79. }
  80. //////////////////////////////
  81. void fillTree(DefaultMutableTreeNode root, String filename)
  82. {
  83. File temp=new File(filename);
  84. if(!(temp.exists() && temp.isDirectory()))
  85.     return;
  86. //System.out.println(filename);
  87. File[] filelist=temp.listFiles();
  88. for(int i=0; i<filelist.length; i++)
  89. {
  90. if(!filelist[i].isDirectory())
  91.     continue;
  92. final DefaultMutableTreeNode tempDmtn=new DefaultMutableTreeNode(filelist[i].getName());
  93. root.add(tempDmtn);
  94. final String newfilename=new String(filename+“\\”+filelist[i].getName());
  95. Thread t=new Thread()
  96. {
  97. public void run()
  98. {
  99. fillTree(tempDmtn,newfilename);
  100. }//run
  101. };//thread
  102. if(t==null)
  103.     {System.out.println(“no more thread allowed “+newfilename);return;}
  104. t.start();
  105. }//for
  106. }//function
  107. //////////////////////////////
  108. void doMouseClicked(MouseEvent me)
  109. {
  110. TreePath tp=tree.getPathForLocation(me.getX(),me.getY());
  111. if(tp==nullreturn;
  112. //jtf.setText(tp.toString());
  113. String s=tp.toString();
  114. s=s.replace(“[“,“”);
  115. s=s.replace(“]”,“”);
  116. s=s.replace(“, “,“\\”);
  117. //s=s.replace(” “,””);
  118. //int z=s.lastIndexOf(“\”\\\””);
  119. //s=”\'”+s; s=s+”\'”;
  120. jtf.setText(s);
  121. showFiles(s);
  122. //java.util.StringTokenizer st=new java.util.StringTokenizer(s,”,”);
  123. //jtf.setText(jtf.getText()+”=”+s);
  124. }
  125. ////////////////////////////////
  126. void showFiles(String filename)
  127. {
  128. File temp=new File(filename);
  129. data=new String[][]{{“”,“”,“”,“”}};
  130. remove(jspTable);
  131. jtb=new JTable(data, colHeads);
  132. jspTable=new JScrollPane(jtb);
  133. setVisible(false);
  134. add(jspTable,BorderLayout.CENTER);
  135. setVisible(true);
  136. if(!temp.exists()) return;
  137. if(!temp.isDirectory()) return;
  138. //System.out.println(filename);
  139. File[] filelist=temp.listFiles();
  140. int fileCounter=0;
  141. data=new String[filelist.length][4];
  142. for(int i=0; i<filelist.length; i++)
  143. {
  144. if(filelist[i].isDirectory())
  145.     continue;
  146. data[fileCounter][0]=new String(filelist[i].getName());
  147. data[fileCounter][1]=new String(filelist[i].length()+“”);
  148. data[fileCounter][2]=new String(!filelist[i].canWrite()+“”);
  149. data[fileCounter][3]=new String(filelist[i].isHidden()+“”);
  150. fileCounter++;
  151. }//for
  152. String dataTemp[][]=new String[fileCounter][4];
  153. for(int k=0; k<fileCounter; k++)
  154.     dataTemp[k]=data[k];
  155. data=dataTemp;
  156. //System.out.println(data);
  157. remove(jspTable);
  158. jtb=new JTable(data, colHeads);
  159. jspTable=new JScrollPane(jtb);
  160. setVisible(false);
  161. add(jspTable,BorderLayout.CENTER);
  162. setVisible(true);
  163. }
  164. ////////////////////////////////
  165. ///////////////////////////////
  166. }
  167. /***********************************/
  168. class ExplorerTest extends JFrame
  169. {
  170. ExplorerTest(String path)
  171. {
  172. super(“Windows Exploder – Javatpoint”);
  173. add(new Explorer(path),“Center”);
  174. setDefaultCloseOperation(EXIT_ON_CLOSE);
  175. setSize(400,400);
  176. setVisible(true);
  177. }
  178. public static void main(String[] args)
  179. {
  180. new ExplorerTest(“.”);
  181. }
  182. }
  183. /***********************************/
download this example
Next TopicPuzzle Game in Java
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Folder Explorer in Java with Source Code Folder Explorer in Java with Source Code: We can develop Folder Explorer in java with the help of IO Stream, AWT/Swing with event handling. Let’s see the code of creating Folder Explorer in java. import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; /***********************************/ class Explorer extends JPanel implements ActionListener { JTextField jtf; JTextArea jta; JTree tree; JButton refresh; JTable jtb; JScrollPane jsp; JScrollPane jspTable; […]

Tags:

....
...