【发布时间】:2015-03-07 14:19:40
【问题描述】:
我有一个显示文件和文件夹的 JTree,我想访问节点以将它们设置为启用或禁用。例如,如果按下按钮,或者当它们的文本等于特定文本时,则将它们设置为禁用。
这是我的主要课程:
public FileViewer(){
frame = new JFrame("File Viewer");
panel = new JPanel(new BorderLayout());
root = new File("D:\\Documents\\A X");
FileSystemModel model = new FileSystemModel(root);
tree = new JTree();
tree.setModel(model);
panel.add(tree, BorderLayout.CENTER);
traverse(tree, "DS.png");
frame.add(panel);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new FileViewer();
}
我可以创建一个遍历方法来检查模型的每个孩子并找到一个特定的孩子:
public void traverse(JTree tree, String word) {
TreeModel model = tree.getModel();
if (model != null) {
Object root = model.getRoot();
//System.out.println("THIS IS ROOOT >>>>>> " + root.toString());
walk(model, root, word);
}
else
System.out.println("Tree is empty.");
}
protected void walk(TreeModel model, Object o, String word){
int cc;
cc = model.getChildCount(o);
for( int i=0; i < cc; i++) {
Object child = model.getChild(o, i);
if (model.isLeaf(child) && child.toString().equals(word)){
System.out.println(child.toString());
}
else {
//System.out.println("--" + child.toString());
walk(model,child, word);
}
}
}
JTree 有方法setEnabled(Boolean),但它的节点没有。有没有办法让节点被禁用?
这是我的FileSystemModel 文件,如果你想知道的话。
【问题讨论】: