JTree代码不起作用

时间:2014-05-27 12:53:37

标签: java swing jtree

我对Java知之甚少,而且我花了很多天试图找出我做错了什么。我有一个名为FileManagerFrame的JFrame表单和一个内部有JTree的Bean表单。在JTree中我有这段代码:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package FileManager;

import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * 
 */
public class FilesTree extends JTree 
{
private File dir;

/**
 * Creates new form FilesTree
 */
public FilesTree() 
{
    initComponents();
    //FilesTree tree = new FilesTree();
    File fList[] = File.listRoots();
      for(int i = 0; i  < fList.length; i++)
      {
          dir = fList[i];
          addNodes(null, dir);
      }


    // Add a listener
    addTreeSelectionListener(new TreeSelectionListener() 
    {
        public void valueChanged(TreeSelectionEvent e) 
        {
            DefaultMutableTreeNode node =(DefaultMutableTreeNode)e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }
    });
}

/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) 
{
   // String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(dir);
    if (curTop != null) 
    { // should only be null at root
        curTop.add(curDir);
    }
    Vector ol = new Vector();
    String[] tmp = dir.list();
    for (int i = 0; i < tmp.length; i++)
    {
        ol.addElement(tmp[i]);
    }
    Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
    File f;
    Vector files = new Vector();
    // Make two passes, one for Dirs and one for Files. This is #1.
    for (int i = 0; i < ol.size(); i++) 
    {
        String thisObject = (String) ol.elementAt(i);
        String newPath;
        if  (".".equals(dir.getName()))
        {
            newPath = thisObject;
        }
        else
        {
            newPath = dir + File.separator + thisObject;
        }
        if ((f = new File(newPath)).isDirectory())
        {
            addNodes(curDir, f);
        }
        else
        {
            files.addElement(thisObject);
        }
    }
    // Pass two: for files.
    for (int fnum = 0; fnum < files.size(); fnum++)
    {
        curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
    }
    return curDir;
}

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

它假设在JTree中显示所有系统根文件。任何人都可以告诉我为什么它不起作用并建议我能做什么?

这是堆栈跟踪:http://prntscr.com/3n6st2

1 个答案:

答案 0 :(得分:1)

我认为您应该阅读有关Java和OOP的更多信息。由于无休止的递归,您的代码片段将引发堆栈溢出。

public FilesTree() 
{
    initComponents();
    FilesTree tree = new FilesTree();

首先,你应该删除最后一行和变量树的所有出现。如果您的代码仍无效,请提供更多信息(例如堆栈跟踪),如果您想获得答案。