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(fol der.getPat h());
createNodes(root);
JTree tree = new JTree(root);
tree.putClientProperty("JT ree.lineSt yle", "Angled");
pnlMain.add(split = new JSplitPane(JSplitPane.HORI ZONTAL_SPL IT, jspTree = new JScrollPane(tree), jspDisp = new JScrollPane(txtpane = new JTextPane())));
split.setDividerLocation(2 25);
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(DefaultMutable TreeNode root)
{
File[] list = folder.listFiles();
for(int i=0; i<list.length; i++)
{
if(list[i].isFile())
{
branch = new DefaultMutableTreeNode(lis t[i]);
root.add(branch);
}
if(list[i].isDirectory())
{
branch = new DefaultMutableTreeNode(lis t[i]);
root.add(branch);
File[] list1 = list[i].listFiles();
for(int j=0; j<list1.length; j++)
{
branch1 = new DefaultMutableTreeNode(lis t1[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.showMessageDia log(null, branch1, "Mouse Cick", JOptionPane.INFORMATION_ME SSAGE); // this is just for debugging purpose
}
public static void main(String args[])
{ new JavaExplorer(); }
}
========================== ========== ========== ========== ========== ========== ========== =====
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(fol
createNodes(root);
JTree tree = new JTree(root);
tree.putClientProperty("JT
pnlMain.add(split = new JSplitPane(JSplitPane.HORI
split.setDividerLocation(2
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(DefaultMutable
{
File[] list = folder.listFiles();
for(int i=0; i<list.length; i++)
{
if(list[i].isFile())
{
branch = new DefaultMutableTreeNode(lis
root.add(branch);
}
if(list[i].isDirectory())
{
branch = new DefaultMutableTreeNode(lis
root.add(branch);
File[] list1 = list[i].listFiles();
for(int j=0; j<list1.length; j++)
{
branch1 = new DefaultMutableTreeNode(lis
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.showMessageDia
}
public static void main(String args[])
{ new JavaExplorer(); }
}
==========================
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did this work for you?? If not, drop a line and I'll see if I can help...
--Steve
--Steve
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
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
--Steve
ASKER
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
killi
I don't know about the expansion of the tree.
But I hope that I helped you think about it.
Tom