为什么不将JButton添加到DefaultTreeCellRenderer显示中?

时间:2019-09-30 14:23:52

标签: java swing user-interface

我写了一个DefaultTreeCellRenderer的子类,在方法getTreeCellRendererComponent中,我向DefaultTreeCellRenderer对象中添加了一个JButton,我希望看到JTree的每个节点都有一个JButton呈现。但JButton不显示,请参见代码和屏幕截图。预先感谢!

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

public class TreeDemo{
    TreeDemo(){
        JFrame jFrame = new JFrame("TreeDemo");
        DefaultMutableTreeNode rootNode = initNodes();
        JTree jTree = new JTree(rootNode);
        jTree.setCellRenderer(new CustomRenderer());
        jFrame.getContentPane().add(jTree);
        jFrame.setMinimumSize(new Dimension(400, 300));
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }

    DefaultMutableTreeNode initNodes(){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
        DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode("sub1");
        DefaultMutableTreeNode sub2 = new DefaultMutableTreeNode("sub1");
        DefaultMutableTreeNode sub1_1 = new DefaultMutableTreeNode("sub1_1");
        DefaultMutableTreeNode sub1_2 = new DefaultMutableTreeNode("sub1_2");
        DefaultMutableTreeNode sub2_1 = new DefaultMutableTreeNode("sub2_1");
        DefaultMutableTreeNode sub2_2 = new DefaultMutableTreeNode("sub2_2");
        root.add(sub1);
        root.add(sub2);
        sub1.add(sub1_1);
        sub1.add(sub1_2);
        sub2.add(sub2_1);
        sub2.add(sub2_2);

        return root;
    }

    class CustomRenderer extends DefaultTreeCellRenderer{
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            JButton button = new JButton("Button");
            setLayout(new BorderLayout());
            add(button, BorderLayout.LINE_END);
            setText("Node");

            return this;
        }
    }

    public static void main(String[] args) {
        new TreeDemo();
    }
}

Screenshot of the result of the code above

1 个答案:

答案 0 :(得分:0)

尽管JLabel扩展了java.awt.Container,但我认为您不能像JPanel那样使用它。

树单元渲染器必须实现接口javax.swing.tree.TreeCellRenderer。它不需要扩展DefaultTreeCellRenderer。还要注意,JTree将使用相同的渲染器实例来渲染所有树节点。

以下代码是您的代码,但是具有不同的树单元格渲染器。它会创建一个JButton,并为每个树节点显示该按钮。

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import java.awt.Component;
import java.awt.Dimension;

public class TreeDemo{
    TreeDemo(){
        JFrame jFrame = new JFrame("TreeDemo");
        DefaultMutableTreeNode rootNode = initNodes();
        JTree jTree = new JTree(rootNode);
        jTree.setCellRenderer(new ButtonRenderer());
        jFrame.getContentPane().add(jTree);
        jFrame.setMinimumSize(new Dimension(400, 300));
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }

    DefaultMutableTreeNode initNodes(){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
        DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode("sub1");
        DefaultMutableTreeNode sub2 = new DefaultMutableTreeNode("sub1");
        DefaultMutableTreeNode sub1_1 = new DefaultMutableTreeNode("sub1_1");
        DefaultMutableTreeNode sub1_2 = new DefaultMutableTreeNode("sub1_2");
        DefaultMutableTreeNode sub2_1 = new DefaultMutableTreeNode("sub2_1");
        DefaultMutableTreeNode sub2_2 = new DefaultMutableTreeNode("sub2_2");
        root.add(sub1);
        root.add(sub2);
        sub1.add(sub1_1);
        sub1.add(sub1_2);
        sub2.add(sub2_1);
        sub2.add(sub2_2);

        return root;
    }

    public static void main(String[] args) {
        new TreeDemo();
    }
}

class ButtonRenderer implements TreeCellRenderer {
    private JButton  button;

    public ButtonRenderer() {
        button = new JButton("Button");
    }

    @Override
    public Component getTreeCellRendererComponent(JTree    tree,
                                                  Object   value,
                                                  boolean  selected,
                                                  boolean  expanded,
                                                  boolean  leaf,
                                                  int      row,
                                                  boolean  hasFocus) {
        return button;
    }
}

这是正在运行的应用程序的屏幕截图...

tree demo