Link to home
Start Free TrialLog in
Avatar of killisrinivas
killisrinivas

asked on

Developing Java Explorer

===========================================================================================

im trying to build a Java Explorer(a la Windows Explorer). Ths following code goes to some extent. im getting 2 problems :

=> The Folders and Files are not in alphabetic order. how do i get it ??(folders first and then files)
=> im able to expand the tree to only one branch. how can i expand it to 'n' branches ??

plzz somebody help me. :(

killi

===========================================================================================

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;

public class JavaExplorer extends JFrame implements MouseListener
 {
   JPanel pnlTop, pnlMain;
   JTextPane txtpane;
   JButton btn;
   JSplitPane split;
   JScrollPane jspTree, jspDisp;
   JTree tree;
   DefaultMutableTreeNode root, branch, branch1;

   File folder = new File("d:/Killi/Java/Swings");

   Container c;

   public JavaExplorer()
    {
      c = getContentPane();
      c.add(pnlTop  = new JPanel(), "North");
      c.add(pnlMain = new JPanel());
      pnlMain.setLayout(new BorderLayout());
      pnlTop.add(btn = new JButton("Click"));

      root = new DefaultMutableTreeNode(folder.getPath());
      createNodes(root);
      JTree tree = new JTree(root);
      tree.putClientProperty("JTree.lineStyle", "Angled");
      pnlMain.add(split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jspTree = new JScrollPane(tree), jspDisp = new JScrollPane(txtpane = new JTextPane())));
      split.setDividerLocation(225);

      setSize(500,300);
      setLocation(100,100);
      setVisible(true);
      setTitle("Java Explorer");

      tree.addMouseListener(this);
      addWindowListener(new WindowAdapter()
       {
          public void windowClosing(WindowEvent we)
           { System.exit(0); }
       });
    }

   public void createNodes(DefaultMutableTreeNode root)
    {
      File[] list = folder.listFiles();

      for(int i=0; i<list.length; i++)
       {
         if(list[i].isFile())
          {
            branch = new DefaultMutableTreeNode(list[i]);
            root.add(branch);
          }

         if(list[i].isDirectory())
          {
            branch = new DefaultMutableTreeNode(list[i]);
            root.add(branch);

            File[] list1 = list[i].listFiles();

            for(int j=0; j<list1.length; j++)
             {
               branch1 = new DefaultMutableTreeNode(list1[j]);
               branch.add(branch1);
             }
          }
       }
    }

   public void mouseEntered(MouseEvent me) {}
   public void mouseExited(MouseEvent me) {}
   public void mousePressed(MouseEvent me) {}
   public void mouseReleased(MouseEvent me) {}
   public void mouseClicked(MouseEvent me)
    {
      JOptionPane.showMessageDialog(null, branch1, "Mouse Cick", JOptionPane.INFORMATION_MESSAGE); // this is just for debugging purpose
    }

   public static void main(String args[])
    { new JavaExplorer(); }
 }

===========================================================================================
Avatar of tomboshell
tomboshell

For the alphabetical seperation I would say to break to another method after you get your File array.  In the new method create two seperate file arrays.  One would hold all the directoris the other all the files.  Iterate through your main array and seperate the files into the two other arrays, and branch to an alphabetizing method (recursive would be best).  Then when done seperating and alphabetizing, add then to the tree in order.  

I don't know about the expansion of the tree.

But I hope that I helped you think about it.
Tom
ASKER CERTIFIED SOLUTION
Avatar of sdussinger
sdussinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did this work for you?? If not, drop a line and I'll see if I can help...

--Steve
Avatar of killisrinivas

ASKER

hi steve,

  first i'd like to apologise that i didn't give response to ur ans immediately. actually i got a bit busy and couldn't get time to check EE.

   i ran ur code. it was brilliant. i just have to work a bit more on it to display the contents selected on the left column into the right column(if possible the content of the file and images too). i'll do it. thank u very much for the code.

instead of  File folder = new File("."); if u write as File folder = new File("\"); then root directories will be listed. that was very nice of u.

   right now im working on JTable where i give data into the cells and then the data from the rows r updated into the database. i got a problem in this too. i have posed this q at EE with subject as "JTable to database". i'd appreciate if u could help me out in this too.

regards,
killi
Cool. Glad I could help. Go ahead and close this one out, while I take a look at the other one. I'll help if I can.

--Steve
thank u very much steve. im still working on JTable with database. i'll get to u soon. im a busy bee now-a-days.

killi