Wednesday, April 8, 2020

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

No comments:

Post a Comment

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...