如何禁用JTree的叶子

时间:2015-03-07 14:19:41

标签: java swing jtree

我有一个显示文件和文件夹的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文件,如果您想了解它。

1 个答案:

答案 0 :(得分:3)

您应该创建自定义DefaultTreeCellRenderer类并使用它setEnabled方法。

enter image description here

package com.company;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        TreeNode treeNode = new DefaultMutableTreeNode("Test");

        JTree tree = new JTree();
        tree.setModel(new DefaultTreeModel(treeNode));
        tree.setCellRenderer(new CustomDefaultTreeCellRenderer());

        JFrame frame = new JFrame();
        frame.setContentPane(tree);
        frame.setSize(320, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class CustomDefaultTreeCellRenderer extends DefaultTreeCellRenderer {
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            boolean enabled = false; // <-- here is your logic for enable/disable cell

            sel = enabled;
            hasFocus = enabled;

            Component treeCellRendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            treeCellRendererComponent.setEnabled(enabled);

            return treeCellRendererComponent;
        }
    }
}