title: Get multiple root directories on JTree to select the leaf - Swing
body: <p>I have been using JTree to get the <a href="http://i.imgur.com/p7iwhVl.png" rel="nofollow">multiple root directories</a> of the system, using the example FileTreeModel.java to achieve it. And also trying to get the selected leaf (path) but it throws <a href="http://pastebin.com/BemWu8fp" rel="nofollow">cast error</a>, please give me some directions on this, have posted the code so far have tried. Thanks.</p>
  
  <pre><code>import java.io.File;
  
  import javax.swing.JFrame;
  import javax.swing.JScrollPane;
  import javax.swing.JTree;
  import javax.swing.SwingUtilities;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.DefaultTreeModel;
  import javax.swing.*;
  import javax.swing.event.*;
  import javax.swing.tree.*;
  import java.io.File;
  
  public class FileTreeDemo {
     public static void main(String[] args) {
     // Figure out where in the filesystem to start displaying
      File root;
  if (args.length &gt; 0) {
      root = new File(args[0]);
      //System.out.println(args.length);
      }
  else root = new File(System.getProperty("user.home"));
      //System.out.println(args.length);
  
  // Create a TreeModel object to represent our tree of files
  FileTreeModel model = new FileTreeModel(root);
  
  
  // Create a JTree and tell it to display our model
  JTree tree = new JTree();
  tree.setModel(model);
  tree.setRootVisible(true);
  tree.setShowsRootHandles(true);
  //tree.setShowsRootHandles(true);
  
  // The JTree can get big, so allow it to scroll.
  JScrollPane scrollpane = new JScrollPane(tree);
  
  // Display it all in a window and make the window appear
  JFrame frame = new JFrame("FileTreeDemo");
  frame.getContentPane().add(scrollpane, "Center");
  frame.setSize(400,600);
  frame.setVisible(true);
  
  // Add a listener
  tree.addTreeSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent e) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
          .getPath().getLastPathComponent();
      System.out.println("You selected " + node);
        }
      });
  
    }
  }
  
  class FileTreeModel implements TreeModel {
    protected File root;
    public FileTreeModel(File root) { this.root = root; }
  
    public Object getRoot() { return root; }
  
    public boolean isLeaf(Object node) {  return ((File)node).isFile(); }
  
    public int getChildCount(Object parent) {
       String[] children = ((File)parent).list();
      if (children == null) return 0;
      return children.length;
    }
  
    public Object getChild(Object parent, int index) {
      String[] children = ((File)parent).list();
      if ((children == null) || (index &gt;= children.length)) return null;
      return new File((File) parent, children[index]);
    }
  
    public int getIndexOfChild(Object parent, Object child) {
      String[] children = ((File)parent).list();
      if (children == null) return -1;
      String childname = ((File)child).getName();
      for(int i = 0; i &lt; children.length; i++) {
        if (childname.equals(children[i])) return i;
      }
      return -1;
    }
  
    public void valueForPathChanged(TreePath path, Object newvalue) {}
  
  
    public void addTreeModelListener(TreeModelListener l) {}
    public void removeTreeModelListener(TreeModelListener l) {}
  }
  </code></pre>