We help IT Professionals succeed at work.

create a tree structure (like windows explorer) in java-swing?

k_s_ashok asked
Medium Priority
Last Modified: 2013-11-23
my project technology : java - swing
how to create a tree structure (like windows explorer) in java-swing?
( need the source code)
Watch Question

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

/** Example tree built out of DefaultMutableTreeNodes.
 *  1999 Marty Hall, http://www.apl.jhu.edu/~hall/java/

public class SimpleTree extends JFrame {
  public static void main(String[] args) {
    new SimpleTree();
  public SimpleTree() {
    super("Creating a Simple JTree");
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    Object[] hierarchy =
      { "javax.swing",
        new Object[] { "javax.swing.plaf",
                       "javax.swing.plaf.multi" },
        new Object[] { "javax.swing.text",
                       new Object[] { "javax.swing.text.html",
                                      "javax.swing.text.html.parser" },
                       "javax.swing.text.rtf" },
        "javax.swing.undo" };
    DefaultMutableTreeNode root = processHierarchy(hierarchy);
    JTree tree = new JTree(root);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    setSize(275, 300);

  /** Small routine that will make node out of the first entry
   *  in the array, then make nodes out of subsequent entries
   *  and make them child nodes of the first one. The process is
   *  repeated recursively for entries that are arrays.
  private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
    DefaultMutableTreeNode node =
      new DefaultMutableTreeNode(hierarchy[0]);
    DefaultMutableTreeNode child;
    for(int i=1; i<hierarchy.length; i++) {
      Object nodeSpecifier = hierarchy[i];
      if (nodeSpecifier instanceof Object[])  // Ie node with children
        child = processHierarchy((Object[])nodeSpecifier);
        child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf

import java.awt.*;
import java.awt.event.*;

/** A listener that you attach to the top-level Frame or JFrame of
 *  your application, so quitting the frame exits the application.
 *  1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/

public class ExitListener extends WindowAdapter {
  public void windowClosing(WindowEvent event) {
import javax.swing.*;
import java.awt.*;

/** A few utilities that simplify using windows in Swing.
 *  1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/

public class WindowUtilities {

  /** Tell system to use native look and feel, as in previous
   *  releases. Metal (Java) LAF is the default otherwise.

  public static void setNativeLookAndFeel() {
    try {
    } catch(Exception e) {
      System.out.println("Error setting native LAF: " + e);

  public static void setJavaLookAndFeel() {
    try {
    } catch(Exception e) {
      System.out.println("Error setting Java LAF: " + e);

   public static void setMotifLookAndFeel() {
    try {
    } catch(Exception e) {
      System.out.println("Error setting Motif LAF: " + e);

  /** A simplified way to see a JPanel or other Container.
   *  Pops up a JFrame with specified Container as the content pane.

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height,
                                    String title,
                                    Color bgColor) {
    JFrame frame = new JFrame(title);
    frame.setSize(width, height);
    frame.addWindowListener(new ExitListener());

  /** Uses Color.white as the background color. */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height,
                                    String title) {
    return(openInJFrame(content, width, height, title, Color.white));

  /** Uses Color.white as the background color, and the
   *  name of the Container's class as the JFrame title.

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height) {
    return(openInJFrame(content, width, height,

This question is LOCKED with a Proposed Answer.  If it helps you, great, accept it and grade it to close.  If not, reject it and comment as to why or what else is needed.

Few additional experts will join this collaboration effort, once a question has been locked.  Just want to confirm this to you.  If more than one expert helps you, you can always split points or award additional help in a new question, within the same topic area.  If you need help from us, post a zero point question here, and include the link:


Community Support Moderator @ Experts Exchange
Force Accepted

Community Support Moderator @Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.