我正在使用Swing开发GUI。 GUI的实现方式是,当用户想要加载一组文本文件时,应打开filechooser对话框,然后用户选择其中包含所有文件的目录。现在,我想使用JTree将所选目录中的所有文件列出到JScrollPane中。我正在使用此示例在我的代码中实现JTree:http://www.java2s.com/Code/Java/File-Input-Output/FileTreeDemo.htm
但是,选择目录后,JTree上不显示JTree。我已经将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) {}
}
重新验证内容容器后,GUI如下所示:
您尚未重新验证包含的content
容器。在actionPerformed方法的末尾,添加以下行
//content.invalidate();
content.revalidate();
content.repaint();
如果需要递归列出目录,甚至需要进行复制,粘贴(管理文件)之类的操作,则>]
如果您正在开发需要项目文件管理器的应用程序(或IDE),则为此目的而开发此组件:
您可以在github上查看代码:https://github.com/ricardojlrufino/JExplorerTree
功能
-延迟加载(仅在扩展时)-没有额外的依赖关系(扩展JPanel并使用JTree)-外部拖放(往返操作系统)-内部拖放(在树内)-键盘支持:F2,DELETE,ENTER-CTRL + C,CTRL + V,CTRL + X(内部)-CTRL + V(来自外部文件)-CTRL + C(允许将JTree形式复制到外部)-应对外部变化(灰显)-本机图标支持-基本操作