Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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在JPanel中显示目录中的文件?_Java_Swing_User Interface_Jtree_Jtreetable - Fatal编程技术网

Java 如何使用JTree在JPanel中显示目录中的文件?

Java 如何使用JTree在JPanel中显示目录中的文件?,java,swing,user-interface,jtree,jtreetable,Java,Swing,User Interface,Jtree,Jtreetable,我正在使用Swing开发GUI。GUI的实现方式是,当用户希望加载一组文本文件时,应打开文件选择器对话框,用户选择所有文件所在的目录。现在,我想使用JTree将所选目录中的所有文件列在一个JScrollPane中。我使用此示例在代码中实现JTree: 但是,在我选择目录后,JTree不会显示在JPanel中。我已经将JTree代码放在actionPerformed()方法中。我不确定这是不是正确的方法 代码如下: public void actionPerformed(ActionEvent

我正在使用Swing开发GUI。GUI的实现方式是,当用户希望加载一组文本文件时,应打开文件选择器对话框,用户选择所有文件所在的目录。现在,我想使用JTree将所选目录中的所有文件列在一个JScrollPane中。我使用此示例在代码中实现JTree:

但是,在我选择目录后,JTree不会显示在JPanel中。我已经将JTree代码放在actionPerformed()方法中。我不确定这是不是正确的方法

代码如下:

public void actionPerformed(ActionEvent e) {

        //Handle open button action.
        if (e.getSource() == OpenFileButton) {
            int returnVal = fc.showOpenDialog(GUIMain.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                System.out.println(file);
                File[] filesInDirectory = file.listFiles();

                SortFile sf = new SortFile();

                // Calls sortByNumber method in class SortFile to list the files number wise
                filesInDirectory = sf.sortByNumber(filesInDirectory);

                FileTreeModel model = new FileTreeModel(file);
                tree = new JTree();
                tree.setModel(model);    
                spectralFilesScrollPane = new JScrollPane(tree);
                //  add(BorderLayout.CENTER, spectralFilesScrollPane);
                spectralFilesScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                spectralFilesScrollPane.setPreferredSize(new Dimension(250, 145));
                spectralFilesScrollPane.setMinimumSize(new Dimension(10, 10));
                spectralFilesScrollPane.setBorder(
                        BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Spectral Files"),
                                BorderFactory.createEmptyBorder(5, 5, 5, 5)));

                content.add(spectralFilesScrollPane);
                spectralFilesScrollPane.setVisible(true);
            }
        }
    }
FileTreeModel.java类

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.*;
import javax.swing.tree.TreePath;
import java.io.File;

/**
 * The methods in this class allow the JTree component to traverse
 * the file system tree, and display the files and directories.
 **/
class FileTreeModel implements TreeModel {

    // We specify the root directory when we create the model.
    protected File root;
    public FileTreeModel(File root) {
        System.out.println("I am in FileTree Model");
        this.root = root; }

    // The model knows how to return the root object of the tree
    public Object getRoot() { return root; }

    // Tell JTree whether an object in the tree is a leaf or not
    public boolean isLeaf(Object node) {  return ((File)node).isFile(); }

    // Tell JTree how many children a node has
    public int getChildCount(Object parent) {
        String[] children = ((File)parent).list();
        if (children == null) return 0;
        System.out.println("printing child length:" + children.length);
        return children.length;
    }

    // Fetch any numbered child of a node for the JTree.
    // Our model returns File objects for all nodes in the tree.  The
    // JTree displays these by calling the File.toString() method.
    public Object getChild(Object parent, int index) {
        String[] children = ((File)parent).list();
        if ((children == null) || (index >= children.length)) return null;
        return new File((File) parent, children[index]);
    }

    // Figure out a child's position in its parent node.
    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 < children.length; i++) {
            if (childname.equals(children[i])) return i;
        }
        return -1;
    }

    // This method is only invoked by the JTree for editable trees.
    // This TreeModel does not allow editing, so we do not implement
    // this method.  The JTree editable property is false by default.
    public void valueForPathChanged(TreePath path, Object newvalue) {}

    // Since this is not an editable tree model, we never fire any events,
    // so we don't actually have to keep track of interested listeners.
    public void addTreeModelListener(TreeModelListener l) {}
    public void removeTreeModelListener(TreeModelListener l) {}
}
import javax.swing.event.TreeModelListener;
导入javax.swing.tree.TreeModel;
导入javax.swing.*;
导入javax.swing.tree.TreePath;
导入java.io.File;
/**
*此类中的方法允许JTree组件遍历
*文件系统树,并显示文件和目录。
**/
类FileTreeModel实现TreeModel{
//我们在创建模型时指定根目录。
受保护的文件根;
公共文件树模型(文件根){
System.out.println(“我在文件树模型中”);
this.root=root;}
//模型知道如何返回树的根对象
公共对象getRoot(){return root;}
//告诉JTree树中的对象是否为叶子
公共布尔isLeaf(对象节点){return((文件)节点).isFile();}
//告诉JTree一个节点有多少子节点
public int getChildCount(对象父对象){
字符串[]子项=((文件)父项).list();
if(children==null)返回0;
System.out.println(“打印子长度:+子长度”);
返回子项长度;
}
//获取JTree节点的任意编号子节点。
//我们的模型返回树中所有节点的文件对象
//JTree通过调用File.toString()方法来显示这些内容。
公共对象getChild(对象父对象,int索引){
字符串[]子项=((文件)父项).list();
if((children==null)| |(index>=children.length))返回null;
返回新文件((文件)父级,子级[索引]);
}
//计算子节点在其父节点中的位置。
public int getIndexOfChild(对象父对象、对象子对象){
字符串[]子项=((文件)父项).list();
if(children==null)返回-1;
字符串childname=((文件)child.getName();
for(int i=0;i
重新验证内容容器后,GUI如下所示:


您尚未重新验证包含
内容的容器。在actionPerformed方法的末尾,添加行

//content.invalidate();
content.revalidate();
content.repaint();

是的,这是我想要的。然而,对于显示器,我得到了一个单独的框,在JPanel Spectrum文件中有滚动条,它是samller。JPanel和JScrollPane的维度在代码中是相同的。我已经更新了关于GUI外观的问题。我能够得到期望的结果。