Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

JTree computer directory browser expand/collapse icon problem

Thread view: 
Jason - 07 Sep 2006 00:34 GMT
Hi guys I recently found this code (and modified it to be in a window
so you can simply compile and run it for your convenience) in one of
the archived read only threads in Sun's Swing GUI forum
(http://forum.java.sun.com/thread.jspa?forumID=257&threadID=165536).
Unfortunately I can't contact the poster because he didn't have any of
his info displaying in his profile and his last post was years ago. The
problem is that when you click on the expand/collapse icon on empty
folders or empty drives such as the floppy or cd drives only then will
the icon disappear.

Does anyone know how I can check the currently expanding node and get
rid of these icons on the directories that don't contain sub
directories before the user clicks on it? Thanks!

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

public class FileTree2 extends JFrame
{
    public static DefaultTreeModel treeModel;
    public FileTree2()
    {
        this.setLayout(new BorderLayout());

        /*Get a list of the root files (drives) on this system. */
        File files[] = File.listRoots();
        JTree.DynamicUtilTreeNode root = new
JTree.DynamicUtilTreeNode("root", files);

        /* Allow each root file to have children. */
        for (Enumeration kids = root.children(); kids.hasMoreElements();)
        {
           JTree.DynamicUtilTreeNode node =
(JTree.DynamicUtilTreeNode)kids.nextElement();
           node.setAllowsChildren(true);
        }

        /* Create a tree of the root files. */
        JTree tree = new JTree(root);
        tree.setRootVisible(false);
        tree.setShowsRootHandles(true);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeExpansionListener(new MyTreeExpansionListener());
        treeModel = (DefaultTreeModel)tree.getModel();

        this.add(new JScrollPane(tree), BorderLayout.CENTER);
        this.pack();
        this.setSize(300,400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args)
        {FileTree2 tree = new FileTree2();}
}

/*
 * A class to handle tree expansion events.
 */
 class MyTreeExpansionListener implements TreeExpansionListener
 {
     private final DirectoryFilter directoryFilter = new
DirectoryFilter();
     public void treeCollapsed(TreeExpansionEvent event) {}
     public void treeExpanded(TreeExpansionEvent event)
     {
           TreePath path = event.getPath();
           JTree.DynamicUtilTreeNode node =
(JTree.DynamicUtilTreeNode)path.getLastPathComponent();
           if (node.getChildCount() == 0)
           {
               File file = (File)node.getUserObject();
               File[] subs = file.listFiles(directoryFilter);
               if ((subs != null) && (subs.length > 0))
               {
                   java.util.Arrays.sort(subs);
                   JTree.DynamicUtilTreeNode.createChildren(node, subs);
                   for (Enumeration kids = node.children();
kids.hasMoreElements();)
                   {
                       JTree.DynamicUtilTreeNode subnode =
(JTree.DynamicUtilTreeNode)kids.nextElement();
                       File subfile = (File)subnode.getUserObject();
                       if (subfile.isDirectory())
                           subnode.setAllowsChildren(true);
                       else
                           subnode.setAllowsChildren(false);
                   }
                   FileTree2.treeModel.reload(node);
               }
           }
     }
 }

/*
 * A class to sift out only directories.
 */
 class DirectoryFilter implements FileFilter
 {
       public boolean accept(File file)
           {return (file.isDirectory() && !file.isHidden()) ? true :
false;}
 }
Jason - 14 Sep 2006 01:03 GMT
It's been a while. Nobody has any ideas?

> Hi guys I recently found this code (and modified it to be in a window
> so you can simply compile and run it for your convenience) in one of
[quoted text clipped - 103 lines]
> false;}
>   }
Babu Kalakrishnan - 14 Sep 2006 06:02 GMT
> Hi guys I recently found this code (and modified it to be in a window
> so you can simply compile and run it for your convenience) in one of
[quoted text clipped - 9 lines]
> rid of these icons on the directories that don't contain sub
> directories before the user clicks on it? Thanks!

This is the normal behaviour of a JTree - it does not query unopened
nodes about whether it has any children before it is actually opened.

I had written a workaround years back - will try to locate it and post
it if I can find it. If not, the general idea of that workaround was to
listen to a nodeExpanded(?) event, and perform an expand and collapse
of all new nodes that just became visible programmatically. Once a node
has been opened, the JTree will remember that state afterwards.

BK
Jason - 29 Sep 2006 21:58 GMT
> > Hi guys I recently found this code (and modified it to be in a window
> > so you can simply compile and run it for your convenience) in one of
[quoted text clipped - 20 lines]
>
> BK

Sorry about my delay on getting back to you. Yes, I see your logic on
going about that, and I may try it out. I hesitate, however, because
when this method would check the computer's floppy drives, it will
create a long pause for each drive. I guess what I might do is use this
only on drives other than the floppies. Would you know of a way to
check to see if a certain root is a floppy drive? I'm also thinking
about customizing the root drive node's icons by having it display its
drive type icon. For instance, nodes that represent floppy drives will
be displayed with a floppy drive icon, hard drives with a hard drive
icon, etc. I know how to customize specific node's icons, but I don't
know how to check to see what type of drive the root node is
representing. I would also use this check to skip the time consuming
task of checking each floppy drive for subdirectories. Any ideas?
Babu Kalakrishnan - 01 Oct 2006 09:54 GMT
>>>Does anyone know how I can check the currently expanding node and get
>>>rid of these icons on the directories that don't contain sub
[quoted text clipped - 16 lines]
> representing. I would also use this check to skip the time consuming
> task of checking each floppy drive for subdirectories. Any ideas?

Check out the API docs of the javax.swing.filechooser.FileSystemView class.

BK
Jason - 05 Oct 2006 19:19 GMT
> >>>Does anyone know how I can check the currently expanding node and get
> >>>rid of these icons on the directories that don't contain sub
[quoted text clipped - 20 lines]
>
> BK

Wow I've never heard of the FileSystemView class before, and it does
look like some of these methods can help me. It looks like I'm good for
now - thanks!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.