JTree中自定义节点上的鼠标操作:jre6和jre7之间的差异

时间:2013-02-15 00:04:04

标签: java swing jbutton jtree treecellrenderer

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

  • 单个JCheckBox
  • 一个JButton
  • 由任何动作组成的JPanel(这里是北方的JCheckBox和南方的JButton)

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

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

除了使用Java Runtime Environment 7之外,当我点击JPanel节点内的JButton时出现问题。第一次单击JPanel时(并且每次停止编辑JPanel的节点后)都不会呈现JButton的“单击”动画。第一个节点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();
            }
        });     
    }
}

0 个答案:

没有答案