具有可选节点的复选框 JTree

Checkbox JTree with selectable nodes

我在这里使用 this example code 创建一个 JTree,其中所有叶子都有一个复选框。我 运行 遇到的问题是我还需要能够 select 节点对象,而不必勾选复选框。在我的用例中,勾选复选框将使某些东西可见或隐藏,但是 select 节点将允许在 UI 的单独部分编辑对象。解决此问题的最佳方法是什么,是否可以检测何时单击复选框的 'box' 部分,否则节点会 selected?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

public class CheckBoxNodeTreeSample {
    public static void main(String args[]) {
            JFrame frame = new JFrame("CheckBox Tree");

            CheckBoxNode accessibilityOptions[] = {
                            new CheckBoxNode(
                                            "Move system caret with focus/selection changes", false),
                            new CheckBoxNode("Always expand alt text for images", true) };
            CheckBoxNode browsingOptions[] = {
                            new CheckBoxNode("Notify when downloads complete", true),
                            new CheckBoxNode("Disable script debugging", true),
                            new CheckBoxNode("Use AutoComplete", true),
                            new CheckBoxNode("Browse in a new process", false) };
            Vector accessVector = new NamedVector("Accessibility",
                            accessibilityOptions);
            Vector browseVector = new NamedVector("Browsing", browsingOptions);
            Object rootNodes[] = { accessVector, browseVector };
            Vector rootVector = new NamedVector("Root", rootNodes);
            JTree tree = new JTree(rootVector);

            CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
            tree.setCellRenderer(renderer);

            tree.setCellEditor(new CheckBoxNodeEditor(tree));
            tree.setEditable(true);

            JScrollPane scrollPane = new JScrollPane(tree);
            frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
            frame.setSize(300, 150);
            frame.setVisible(true);
    }
}

class CheckBoxNodeRenderer implements TreeCellRenderer {
    private JCheckBox leafRenderer = new JCheckBox();

    private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

    Color selectionBorderColor, selectionForeground, selectionBackground,
                    textForeground, textBackground;

    protected JCheckBox getLeafRenderer() {
            return leafRenderer;
    }

    public CheckBoxNodeRenderer() {
            Font fontValue;
            fontValue = UIManager.getFont("Tree.font");
            if (fontValue != null) {
                    leafRenderer.setFont(fontValue);
            }
            Boolean booleanValue = (Boolean) UIManager
                            .get("Tree.drawsFocusBorderAroundIcon");
            leafRenderer.setFocusPainted((booleanValue != null)
                            && (booleanValue.booleanValue()));

            selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
            selectionForeground = UIManager.getColor("Tree.selectionForeground");
            selectionBackground = UIManager.getColor("Tree.selectionBackground");
            textForeground = UIManager.getColor("Tree.textForeground");
            textBackground = UIManager.getColor("Tree.textBackground");
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
                    boolean selected, boolean expanded, boolean leaf, int row,
                    boolean hasFocus) {

            Component returnValue;
            if (leaf) {

                    String stringValue = tree.convertValueToText(value, selected,
                                    expanded, leaf, row, false);
                    leafRenderer.setText(stringValue);
                    leafRenderer.setSelected(false);

                    leafRenderer.setEnabled(tree.isEnabled());

                    if (selected) {
                            leafRenderer.setForeground(selectionForeground);
                            leafRenderer.setBackground(selectionBackground);
                    } else {
                            leafRenderer.setForeground(textForeground);
                            leafRenderer.setBackground(textBackground);
                    }

                    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
                            Object userObject = ((DefaultMutableTreeNode) value)
                                            .getUserObject();
                            if (userObject instanceof CheckBoxNode) {
                                    CheckBoxNode node = (CheckBoxNode) userObject;
                                    leafRenderer.setText(node.getText());
                                    leafRenderer.setSelected(node.isSelected());
                            }
                    }
                    returnValue = leafRenderer;
            } else {
                    returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
                                    value, selected, expanded, leaf, row, hasFocus);
            }
            return returnValue;
    }
}

class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();

    ChangeEvent changeEvent = null;

    JTree tree;

    public CheckBoxNodeEditor(JTree tree) {
            this.tree = tree;
    }

    public Object getCellEditorValue() {
            JCheckBox checkbox = renderer.getLeafRenderer();
            CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(),
                            checkbox.isSelected());
            return checkBoxNode;
    }

    public boolean isCellEditable(EventObject event) {
            boolean returnValue = false;
            if (event instanceof MouseEvent) {
                    MouseEvent mouseEvent = (MouseEvent) event;
                    TreePath path = tree.getPathForLocation(mouseEvent.getX(),
                                    mouseEvent.getY());
                    if (path != null) {
                            Object node = path.getLastPathComponent();
                            if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
                                    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                                    Object userObject = treeNode.getUserObject();
                                    returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
                            }
                    }
            }
            return returnValue;
    }

    public Component getTreeCellEditorComponent(JTree tree, Object value,
                    boolean selected, boolean expanded, boolean leaf, int row) {

            Component editor = renderer.getTreeCellRendererComponent(tree, value,
                            true, expanded, leaf, row, true);

            // editor always selected / focused
            ItemListener itemListener = new ItemListener() {
                    public void itemStateChanged(ItemEvent itemEvent) {
                            if (stopCellEditing()) {
                                    fireEditingStopped();
                            }
                    }
            };
            if (editor instanceof JCheckBox) {
                    ((JCheckBox) editor).addItemListener(itemListener);
            }

            return editor;
    }
}

class CheckBoxNode {
    String text;

    boolean selected;

    public CheckBoxNode(String text, boolean selected) {
            this.text = text;
            this.selected = selected;
    }

    public boolean isSelected() {
            return selected;
    }

    public void setSelected(boolean newValue) {
            selected = newValue;
    }

    public String getText() {
            return text;
    }

    public void setText(String newValue) {
            text = newValue;
    }

    public String toString() {
            return getClass().getName() + "[" + text + "/" + selected + "]";
    }
}

class NamedVector extends Vector {
    String name;

    public NamedVector(String name) {
            this.name = name;
    }

    public NamedVector(String name, Object elements[]) {
            this.name = name;
            for (int i = 0, n = elements.length; i < n; i++) {
                    add(elements[i]);
            }
    }

    public String toString() {
            return "[" + name + "]";
    }
}

我看到的另一个错误是当 'folder' 有子元素时,文件夹左侧的指示器正确显示文件夹已打开:

但如果 'folder' 为空,该指示器最初会出现,但当您打开它时它会消失。如果文件夹是空的,我认为这最初不应该出现。

我能够使用@MadProgrammer 的建议来分离复选框和文本,以及删除 CheckBoxNodeEditor 中的树引用,更新后的代码如下:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

public class CheckBoxNodeTreeSample 
{
    
    private JTree tree;

    public static void main(String... s)
    {
        new CheckBoxNodeTreeSample();
    }

    public CheckBoxNodeTreeSample() 
    {
        JFrame frame = new JFrame("CheckBox Tree");
    
        CheckBoxNode accessibilityOptions[] = {
                    new CheckBoxNode(
                                    "Move system caret with focus/selection changes", false),
                    new CheckBoxNode("Always expand alt text for images", true) };
        CheckBoxNode browsingOptions[] = {
                    new CheckBoxNode("Notify when downloads complete", true),
                    new CheckBoxNode("Disable script debugging", true),
                    new CheckBoxNode("Use AutoComplete", true),
                    new CheckBoxNode("Browse in a new process", false) };
        Vector accessVector = new NamedVector("Accessibility",
                    accessibilityOptions);
        Vector browseVector = new NamedVector("Browsing", browsingOptions);
        Object rootNodes[] = { accessVector, browseVector };
        Vector rootVector = new NamedVector("Root", rootNodes);
        tree = new JTree(rootVector);
   
    
        CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
        tree.setCellRenderer(renderer);
        tree.setCellEditor(new CheckBoxNodeEditor());
        tree.setEditable(true);

        JScrollPane scrollPane = new JScrollPane(tree);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setSize(360, 260);
        frame.setVisible(true);
    }
}

class CheckBoxPanel extends JPanel
{
    public JCheckBox checkBox;
    public JLabel label;

    public CheckBoxPanel() 
    { 
        super();
    
        checkBox = new JCheckBox();
        label = new JLabel();
    
        checkBox.setBorder(new EmptyBorder(0, 0, 0, 0));

        add(checkBox);
        add(label);
    }
}

class CheckBoxNodeRenderer implements TreeCellRenderer 
{
    private CheckBoxPanel leafRenderer = new CheckBoxPanel();

    private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

    Color selectionBorderColor, selectionForeground, selectionBackground,
                    textForeground, textBackground;

    protected CheckBoxPanel getLeafRenderer() 
    {
            return leafRenderer;
    }

    public CheckBoxNodeRenderer() {
            Font fontValue;
            fontValue = UIManager.getFont("Tree.font");
            if (fontValue != null) {
                    leafRenderer.checkBox.setFont(fontValue);
                    leafRenderer.label.setFont(fontValue);
            }
            Boolean booleanValue = (Boolean) UIManager
                            .get("Tree.drawsFocusBorderAroundIcon");
            leafRenderer.checkBox.setFocusPainted((booleanValue != null)
                            && (booleanValue.booleanValue()));

            selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
            selectionForeground = UIManager.getColor("Tree.selectionForeground");
            selectionBackground = UIManager.getColor("Tree.selectionBackground");
            textForeground = UIManager.getColor("Tree.textForeground");
            textBackground = UIManager.getColor("Tree.textBackground");
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
                    boolean selected, boolean expanded, boolean leaf, int row,
                    boolean hasFocus) {

            Component returnValue;
            if (leaf) {

                    String stringValue = tree.convertValueToText(value, selected,
                                    expanded, leaf, row, false);
                    
                    leafRenderer.checkBox.setSelected(false);
                    leafRenderer.label.setText(stringValue);

                    leafRenderer.setEnabled(tree.isEnabled());

                    if (selected) {
                            leafRenderer.setForeground(selectionForeground);
                            leafRenderer.setBackground(selectionBackground);
                    } else {
                            leafRenderer.setForeground(textForeground);
                            leafRenderer.setBackground(textBackground);
                    }

                    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
                            Object userObject = ((DefaultMutableTreeNode) value)
                                            .getUserObject();
                            if (userObject instanceof CheckBoxNode) {
                                    CheckBoxNode node = (CheckBoxNode) userObject;                                        
                                    leafRenderer.checkBox.setSelected(node.isSelected());
                                    leafRenderer.label.setText(node.getText());
                            }
                    }
                    returnValue = leafRenderer;
            } else {
                    returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
                                    value, selected, expanded, leaf, row, hasFocus);
            }
            return returnValue;
    }
}

class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();

    ChangeEvent changeEvent = null;

    public Object getCellEditorValue() {
            CheckBoxPanel checkBoxPanel = renderer.getLeafRenderer();
            CheckBoxNode checkBoxNode = new CheckBoxNode(checkBoxPanel.label.getText(),
                    checkBoxPanel.checkBox.isSelected());
            return checkBoxNode;
    }

    public boolean isCellEditable(EventObject event) {
            boolean returnValue = false;
            if (event instanceof MouseEvent) {
                    MouseEvent mouseEvent = (MouseEvent) event;
                    JTree tree = (JTree)event.getSource();

                    TreePath path = tree.getPathForLocation(mouseEvent.getX(),
                                    mouseEvent.getY());
                    if (path != null) {
                            Object node = path.getLastPathComponent();
                            if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
                                    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                                    Object userObject = treeNode.getUserObject();
                                    returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
                            }
                    }
            }
            return returnValue;
    }

    public Component getTreeCellEditorComponent(JTree tree, Object value,
                    boolean selected, boolean expanded, boolean leaf, int row)  {

            Component editor = renderer.getTreeCellRendererComponent(tree, value,
                            true, expanded, leaf, row, true);

            // editor always selected / focused
            ItemListener itemListener = new ItemListener() {
                    public void itemStateChanged(ItemEvent itemEvent) {
                            if (stopCellEditing()) {
                                    fireEditingStopped();
                            }
                    }
            };
            
            if (editor instanceof CheckBoxPanel) 
            {
                ((CheckBoxPanel) editor).checkBox.addItemListener(itemListener);
            }

            return editor;
    }
}

class CheckBoxNode {
    String text;

    boolean selected;

    public CheckBoxNode(String text, boolean selected) {
            this.text = text;
            this.selected = selected;
    }

    public boolean isSelected() {
            return selected;
    }

    public void setSelected(boolean newValue) {
            selected = newValue;
    }

    public String getText() {
            return text;
    }

    public void setText(String newValue) {
            text = newValue;
    }

    public String toString() {
            return getClass().getName() + "[" + text + "/" + selected + "]";
    }
}

class NamedVector extends Vector 
{
    String name;

    public NamedVector(String name) {
            this.name = name;
    }

    public NamedVector(String name, Object elements[]) {
            this.name = name;
            for (int i = 0, n = elements.length; i < n; i++) {
                    add(elements[i]);
            }
    }

    public String toString() {
            return "[" + name + "]";
    }
}