【发布时间】:2014-08-01 01:51:59
【问题描述】:
在我们开始之前,我想指出我对 JTree 及其亲属比较陌生。
我节点中的子节点显示返回其父节点的相对路径,但我不希望它这样做。
这是它的样子
http://puu.sh/azCJa/8dd84029b7.png
我希望它是这样的:
testing 0
>testing 1
>>file 1
不是
testing 0
>testing 0\testing 1
>>...
这是一些可运行的代码。
import java.awt.Color;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.LineBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class NGui {
JFrame frame;
private JTree tree;
public NGui() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 493, 608);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 212, 500);
frame.getContentPane().add(scrollPane);
tree = new JTree(addNodes(null, new File(getWorkPath())));
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.setBorder(new LineBorder(new Color(0, 0, 0)));
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
scrollPane.setViewportView(tree);
tree.getSelectionModel().addTreeSelectionListener(
new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
treeValueChanged(e);
}
});
}
private String getWorkPath() {
return System.getProperty("user.home") + "\\Program Name\\";
}
private void treeValueChanged(TreeSelectionEvent e) {
}
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode top, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode root = new DefaultMutableTreeNode(
curPath.replace(getWorkPath(), ""));
if (top != null) { // should only be null at root
log("Adding curPath: " + root);
top.add(root);
}
Vector<String> ol = new Vector<String>();
String[] tmp = dir.list();
for (String s : tmp) {
log("Adding file: " + s);
ol.addElement(s);
}
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File file;
Vector<String> files = new Vector<String>();
// for dirs
for (int i = 0; i < ol.size(); i++) {
String filePath = ol.elementAt(i);
String newPath;
if (curPath.equals(".")) // if root
newPath = filePath;
else
// not root
newPath = curPath + File.separator + filePath;
file = new File(newPath);
log(String.format("cur: %s | file: %s | new: %s", curPath,
filePath, newPath));
// if not a file then go inside folder and get files
if (file.isDirectory())
addNodes(root, file);
else if (filePath.contains(".txt")) // txt files only
files.addElement(filePath);
}
// for files
for (int fnum = 0; fnum < files.size(); fnum++)
root.add(new DefaultMutableTreeNode(files.elementAt(fnum).replace(getWorkPath(), ""), true));
return root;
}
public void log(Object o) {
System.out.println(o);
}
}
【问题讨论】:
-
考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应
-
我会更仔细、非常仔细地查看
newPath = curPath + File.separator + filePath。在代码中的这一点添加断点并启动 IDE 的调试器并检查此值的结果并查看您发现的内容... -
我已经看过了,但仍然不太明白问题所在。更改那里的任何内容都会导致 StackOverflow 或停止列出节点中的子节点。为问题添加一个可运行的示例