Re: JTree computer directory browser expand/collapse icon problem



It's been a while. Nobody has any ideas?

Jason wrote:
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;}
}

.



Relevant Pages

  • JTree computer directory browser expand/collapse icon problem
    ... the icon disappear. ... public class FileTree2 extends JFrame ... JTree tree = new JTree; ... public void treeCollapsed ...
    (comp.lang.java.help)
  • Exception Removing Tabs from JTabbedPane
    ... includes an X icon for deleting tabs. ... public void addTab{ ... String tip, int index) { ...
    (comp.lang.java.gui)
  • Re: valueForPathChanged event
    ... model for the tree and display the data in the gui in the form of a ... listenerList.addElement(listener); ... public void removeTreeModelListener{ ... (TreeModelListener) ...
    (comp.lang.java.programmer)
  • Re: [kde] Trying to control Konq profile
    ... I can get tree views just fine. ... I just fired up Fedora 9, and opened konqueror, and the page you're ... Konqueror in file manager mode accessable from the icon to the ... Click on the System Menu icon and go for the home folder. ...
    (KDE)
  • Re: how do i get the computer to read out what i have typed
    ... "Suzanne S. Barnhill" wrote: ... Microsoft MVP (Word) ... Click on that icon, then click Tools> ... the Normal icon in the tree will still be selected. ...
    (microsoft.public.word.docmanagement)