如何禁用 JTree 的叶子

How to disable a leaf of the JTree

我有一个显示文件和文件夹的 JTree,我想访问节点以将它们设置为启用或禁用。例如,如果按下按钮,或者当它们的文本等于特定文本时,则将它们设置为禁用。

这是我的主要内容 class:

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 文件,如果你想知道的话。

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

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