public boolean expand(DefaultMutableTreeNode parent)
{
DefaultMutableTreeNode flag =
(DefaultMutableTreeNode)parent.getFirstChild();
if (flag==null) // No flag
return false;
Object obj = flag.getUserObject();
if (!(obj instanceof Boolean))
return false; // Already expanded
parent.removeAllChildren(); // Remove Flag
File[] files = listFiles();
if (files == null)
return true;
Vector v = new Vector();
for (int k=0; k<files.length; k++)
{
File f = files[k];
if (!(f.isDirectory()))
continue;
public class FileTree1
extends JFrame
{
public static final ImageIcon ICON_COMPUTER =
new ImageIcon("computer.gif");
public static final ImageIcon ICON_DISK =
new ImageIcon("disk.gif");
public static final ImageIcon ICON_FOLDER =
new ImageIcon("folder.gif");
public static final ImageIcon ICON_EXPANDEDFOLDER =
new ImageIcon("expandedfolder.gif");
// Make sure expansion is threaded and updating the tree model
// only occurs within the event dispatching thread.
class DirExpansionListener implements TreeExpansionListener
{
public void treeExpanded(TreeExpansionEvent event)
{
final DefaultMutableTreeNode node = getTreeNode(
event.getPath());
final FileNode fnode = getFileNode(node);
Thread runner = new Thread()
{
public void run()
{
if (fnode != null && fnode.expand(node))
{
Runnable runnable = new Runnable()
{
public void run()
{
m_model.reload(node);
}
};
SwingUtilities.invokeLater(runnable);
}
}
};
runner.start();
}
public void treeCollapsed(TreeExpansionEvent event) {}
}
class DirSelectionListener
implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent event)
{
DefaultMutableTreeNode node = getTreeNode(
event.getPath());
FileNode fnode = getFileNode(node);
if (fnode != null)
m_display.setText(fnode.getFile().
getAbsolutePath());
else
m_display.setText("");
}
}
public static void main(String argv[])
{
new FileTree1();
}
}
class IconCellRenderer
extends JLabel
implements TreeCellRenderer
{
protected Color m_textSelectionColor;
protected Color m_textNonSelectionColor;
protected Color m_bkSelectionColor;
protected Color m_bkNonSelectionColor;
protected Color m_borderSelectionColor;