2

JTree 使用 TreeCellRenderer 和 TreeCellEditor 显示三个自定义节点:

  • 单个 JCheckBox
  • 单个 JButton
  • 由任何操作组件组成的 JPanel(这里是 North 上的 JCheckBox 和 South 上的 JButton)

截图:http: //i.imgur.com/9j6B2ji.png

如果使用 Java Runtime Environment 6,这样的树没有问题。

除了使用 Java Runtime Environment 7,当我单击 JPanel 节点内的 JButton 时,也会出现问题。JButton 的“单击”动画不会在您第一次单击 JPanel 时呈现(以及每次停止编辑具有 JPanel 的节点之后)。第一个节点 JCheckBox 和第二个节点 JButton 也可以与 jre6 和 jre7 完美配合。

我想让它在 jre6 中完全像以前一样工作,有解决方法吗?

注意:让按钮执行代码不是问题。

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

class TreeNodeModel{
    public boolean isJCheckBox = false;
    public boolean isJButton = false;
    public boolean isJPanel = false;
    public Component userObject = null;
    public TreeNodeModel(String name){
        if( name.equals("JCheckBox") ){
            isJCheckBox = true;
        }else if( name.equals("JButton") ){
            isJButton = true;
        }else if( name.equals("JPanel") ){
            isJPanel = true;
        }
    }
}

class CustomTreeCellRenderer implements TreeCellRenderer{
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component returnValue = null;
        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof TreeNodeModel) {
                TreeNodeModel node = (TreeNodeModel) userObject;
                if(node.isJButton){
                    returnValue = new JButton("Ok");
                }else if(node.isJCheckBox){
                    returnValue = new JCheckBox("Ok");
                }else{
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(new JButton("Problem"), BorderLayout.SOUTH);
                    panel.add(new JCheckBox("Problem"), BorderLayout.NORTH);
                    returnValue = panel;
                }
            }
        }
        if(returnValue == null)
            returnValue = new DefaultTreeCellRenderer().getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        return returnValue;
    }
}

class CustomTreeCellEditor extends AbstractCellEditor implements TreeCellEditor {
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row) {
        Component returnValue = null;
        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof TreeNodeModel) {
                TreeNodeModel node = (TreeNodeModel) userObject;
                if(node.isJButton){
                    returnValue = new JButton("Ok");
                }else if(node.isJCheckBox){
                    returnValue = new JCheckBox("Ok");
                }else{
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(new JButton("Problem"), BorderLayout.SOUTH);
                    panel.add(new JCheckBox("Problem"), BorderLayout.NORTH);
                    returnValue = panel;
                }
            }
        }
        return returnValue;
    }
    @Override
    public Object getCellEditorValue() { return null; }
}

public class MyTestRendererEditor extends JFrame {
    public MyTestRendererEditor() { 
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("root");
        node.add(new DefaultMutableTreeNode(new TreeNodeModel("JCheckBox")));
        node.add(new DefaultMutableTreeNode(new TreeNodeModel("JButton")));
        node.add(new DefaultMutableTreeNode(new TreeNodeModel("JPanel")));

        final JTree tree = new JTree(new DefaultTreeModel(node));
        tree.setCellRenderer(new CustomTreeCellRenderer());
        tree.setCellEditor(new CustomTreeCellEditor());
        tree.setEditable(true);

        getContentPane().add(new JScrollPane(tree));
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) { 
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MyTestRendererEditor();
            }
        });     
    }
}
4

0 回答 0