gpt4 book ai didi

java - 在 Swing JTree 中隐藏文件和隐藏目录

转载 作者:行者123 更新时间:2023-11-29 05:18:03 24 4
gpt4 key购买 nike

我已经使用 Java2s.com 中的这段代码来显示我的主目录的文件树。我想让用户选择将包含我的程序将使用的信息的目录。我进行了多次尝试(太多无法在此处列出)以使树仅显示非隐藏目录。文件和隐藏目录不会显示在树上。

有没有办法做到这一点?我在这上面花了很多时间,但没有成功。有没有办法拦截最终显示的样子?也许渲染器可以询问树并删除属于 isHidden() 的节点?

public class FileTreeDemo {

static File root;
static FileTreeModel model;

public static void main(String[] args) {
// Figure out where in the filesystem to start displaying

if (args.length > 0) {
root = new File(args[0]);
} else {
root = new File(System.getProperty("user.home"));
}

// Create a TreeModel object to represent our tree of files
model = new FileTreeModel(root);

// Create a JTree and tell it to display our model
JTree tree = new JTree();
tree.setModel(model);

// 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);
}

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

// We specify the root directory when we create the model.
protected File root;

public FileTreeModel(File root) {
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
@Override
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;
}
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.
@Override
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 invoked by the JTree only 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) {
}
}
}

最佳答案

忽略隐藏文件取决于模型。渲染器的工作只是显示,它不应该干扰数据选择和过滤。例如,您可以使用 File.listFiles(FileFilter filter)在模型中:

private static final File[] EMPTY_LIST = {};

private File[] getFiles(File parent) {
File[] files = parent.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return !file.isHidden();
}
});
return files != null ? files : EMPTY_LIST;
}

@Override
public int getChildCount(Object parent) {
return getFiles((File) parent).length;
}

@Override
public Object getChild(Object parent, int index) {
return getFiles((File) parent)[index];
}

@Override
public int getIndexOfChild(Object parent, Object child) {
File[] files = getFiles((File) parent);
for (int idx = 0; idx < files.length; idx++) {
if (files[idx].equals(child))
return idx;
}
return -1;
}

关于java - 在 Swing JTree 中隐藏文件和隐藏目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25825780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com