Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JTree中显示文件夹的内容?_Java_Swing_User Interface - Fatal编程技术网

Java 如何在JTree中显示文件夹的内容?

Java 如何在JTree中显示文件夹的内容?,java,swing,user-interface,Java,Swing,User Interface,我试图实现的是采用预定义的文件路径,并在JTree中显示该文件路径中的每个文件夹和文件 简单地 Folder1 Folder2 Folder3a ->File3a ->File2a ->File2b 到目前为止,我掌握的情况如下: public class GUI extends JPanel { public GUI() { super(new GridLayout(1, 2, 20, 0)); setBorder

我试图实现的是采用预定义的文件路径,并在JTree中显示该文件路径中的每个文件夹和文件

简单地

Folder1
Folder2
    Folder3a
        ->File3a
    ->File2a
    ->File2b
到目前为止,我掌握的情况如下:

public class GUI extends JPanel {

public GUI() {

    super(new GridLayout(1, 2, 20, 0));
    setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (final Exception ignored) {
    }

    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);



    frame.setContentPane(this);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}
我想知道如何进行。到目前为止,所有这些都是我读过的大量阅读资料的汇编,我真的不知道如何实现我的目标。

公共类文件树扩展了JPanel{
public class FileTree extends JPanel {
/** Construct a FileTree */
public FileTree(File dir) {
setLayout(new BorderLayout());

// Make a tree list with all the nodes, and make it a JTree
JTree tree = new JTree(addNodes(null, dir));

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

// Lastly, put the JTree into a JScrollPane.
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
add(BorderLayout.CENTER, scrollpane);
 }

 /** Add nodes from under "dir" into curTop. Highly recursive. */
 DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
 String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
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 (curPath.equals("."))
    newPath = thisObject;
  else
    newPath = curPath + 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;
}

 public Dimension getMinimumSize() {
   return new Dimension(200, 400);
 }

 public Dimension getPreferredSize() {
  return new Dimension(200, 400);
}

/** Main: make a Frame, add a FileTree */
 public static void main(String[] av) {

JFrame frame = new JFrame("FileTree");
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
Container cp = frame.getContentPane();

if (av.length == 0) {
  cp.add(new FileTree(new File(".")));
} else {
  cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
  for (int i = 0; i < av.length; i++)
    cp.add(new FileTree(new File(av[i])));
}

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/**构建一个文件树*/ 公共文件树(文件目录){ setLayout(新的BorderLayout()); //制作一个包含所有节点的树列表,并使其成为JTree JTree树=新的JTree(addNodes(null,dir)); //添加一个侦听器 addTreeSelectionListener(新建TreeSelectionListener()){ 公共作废值已更改(TreeSelection事件e){ DefaultMutableTreeNode=(DefaultMutableTreeNode)e .getPath().getLastPathComponent(); System.out.println(“您选择了”+节点); } }); //最后,将JTree放入JScrollPane中。 JScrollPane scrollpane=新的JScrollPane(); scrollpane.getViewport().add(树); 添加(BorderLayout.CENTER,滚动窗格); } /**将“dir”下的节点添加到curTop中。高度递归*/ DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop,文件目录){ 字符串curPath=dir.getPath(); DefaultMutableTreeNode curDir=新的DefaultMutableTreeNode(curPath); if(curTop!=null){//只应在根处为null curTop.add(curDir); } 向量ol=新向量(); 字符串[]tmp=dir.list(); for(int i=0;i
如果我沿着这条路线走下去,那就又提出了一个问题;1) 如何获取根目录下所有文件的路径?请查看@andrewhompson以了解一些想法