当前位置: 首页 > 知识库问答 >
问题:

如何使JPanel组件作为JTree中的节点可用?

曹镜
2023-03-14

当我点击JButton时,什么都不会发生。这甚至适用于我放入的JScrollPane(它显示,但不会滚动)。为什么JPanel不在前面?我觉得有些东西必须被覆盖。应该是不是箭头部分的展开?如果是,如何做到这一点?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.tree.*;

public class test {

public test() {

    JTree tree = createTree();
    tree.setToggleClickCount(0);
    tree.setRowHeight(50);
    tree.setCellRenderer(new PanelRenderer());

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree));

    f.setSize(400, 400);
    f.setLocation(200, 200);
    f.setVisible(true);
}

private JTree createTree() {
    int children = 4;
    int grandChildren = 3;
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode node;
    for (int j = 0; j < children; j++) {
        node = new DefaultMutableTreeNode();
        root.add(node);
        for (int k = 0; k < grandChildren; k++)
            node.add(new DefaultMutableTreeNode());
    }
    DefaultTreeModel model = new DefaultTreeModel(root);
    return new JTree(model);
}

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

class PanelRenderer implements TreeCellRenderer {
JPanel panel;

public PanelRenderer() {
    panel = new JPanel(new BorderLayout());
}

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

    panel.add(new JLabel("aloha"), BorderLayout.WEST);
    JButton fg = new JButton("fg");
    panel.add(fg, BorderLayout.EAST);
    fg.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent d) {
            System.out.println("hello");
        }
    });
    panel.setPreferredSize(new Dimension(200, 500));
    return panel;
}
}

这是为疯狂程序员说,渲染器和编辑器使你放下的图像。在本例中,JButton是可单击的。

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import java.util.*;

public class test extends JFrame {

public test() {
    super("TreeDemo");
    setSize(500, 500);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());

    JButton theParent = new JButton("Feature Set #1");
    JButton thefeature = new JButton("sdfdsfds");
    JButton feature2 = new JButton("Second Feature");
    JButton feature3 = new JButton("Third Feature");
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(theParent, true);
    DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(thefeature, true);
    DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(feature2, true);
    DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(feature3, false);

    n1.add(n3);
    top.add(n1);
    top.add(n2);
    JTree tree = new JTree(top);
    p.add(tree, BorderLayout.NORTH);
    getContentPane().add(p);
    TestRenderer tr = new TestRenderer();
    TestEditor1 te = new TestEditor1();

    tree.setEditable(true);
    tree.setCellRenderer(tr);
    tree.setCellEditor(te);
}

public class TestEditor1 implements TreeCellEditor {

    public TestEditor1() {
    }

    public void addCellEditorListener(CellEditorListener l) {
    }

    public void cancelCellEditing() {
    }

    public Object getCellEditorValue() {
        return this;
    }

    public boolean isCellEditable(EventObject evt) {
        if (evt instanceof MouseEvent) {
            MouseEvent mevt = (MouseEvent) evt;

            if (mevt.getClickCount() == 1) {
                return true;
            }
        }

        return false;
    }

    public void removeCellEditorListener(CellEditorListener l) {
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    public boolean stopCellEditing() {
        return false;
    }

    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
            boolean leaf, int row) {
        DefaultMutableTreeNode temp = (DefaultMutableTreeNode) value;
        JComponent temp2 = (JComponent) temp.getUserObject();
        return temp2;

    }
}

public class TestRenderer implements TreeCellRenderer {
    transient protected Icon closedIcon;
    transient protected Icon openIcon;

    public TestRenderer() {
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        DefaultMutableTreeNode temp = (DefaultMutableTreeNode) value;
        JComponent temp2 = (JComponent) temp.getUserObject();
        temp2.setBackground(UIManager.getColor("Tree.textBackground"));

        return temp2;
    }

    public void setClosedIcon(Icon newIcon) {
        closedIcon = newIcon;
    }

    public void setOpenIcon(Icon newIcon) {
        openIcon = newIcon;
    }
}

public static void main(String args[]){
JFrame frame = new test();
     frame.addWindowListener(new WindowAdapter()
 {
        public void windowClosing(WindowEvent e) 
{
            System.exit(0);
       }
 });  

frame.pack();
    frame.setVisible(true);
}

共有1个答案

上官凯泽
2023-03-14

首先看一下概念:编辑器和渲染器。

渲染器只是一个“橡皮图章”,用来“盖章”组件正在渲染的数据视图,它不是生命组件。视图中的每个单元格使用相同的渲染实例进行渲染。

另一方面,编辑器是一个实时组件,可以响应用户交互。视图一次只有一个活动编辑器。

您的模型不应该携带组件,它们应该只携带您需要的状态信息,以便呈现(和编辑)它。

import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

public class Test {

    public Test() {

        JTree tree = createTree();
        tree.setToggleClickCount(0);
        tree.setCellRenderer(new StateRenderer());
        tree.setCellEditor(new StateEditor());
        tree.setEditable(true);

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(tree));

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JTree createTree() {
        int children = 4;
        int grandChildren = 2;
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new State("Films", false));
        DefaultMutableTreeNode node;

        String[] cat = {"Sci-Fi", "Fantasy", "Action", "Comedy"};
        String[][] films = {
            {"Star Wars", "Star Trek"},
            {"Lord of the Rings", "Conan"},
            {"Terminator", "Transformers"},
            {"Cheaper by the Doze", "Father of the Bride"}
        };
        for (int j = 0; j < children; j++) {
            node = new DefaultMutableTreeNode(new State(cat[j], false));
            root.add(node);
            for (int k = 0; k < grandChildren; k++) {
                node.add(new DefaultMutableTreeNode(new State(films[j][k], false)));
            }
        }
        DefaultTreeModel model = new DefaultTreeModel(root);
        return new JTree(model);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                new Test();
            }
        });
    }

    public class State {

        private String text;
        private boolean selected;

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

        public String getText() {
            return text;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

    }

    public class StateEditor extends AbstractCellEditor implements TreeCellEditor {

        //JPanel panel;
        private JCheckBox checkBox;

        private State editorValue;

        public StateEditor() {
            checkBox = new JCheckBox();
            checkBox.setOpaque(false);
        }

        @Override
        public Object getCellEditorValue() {
            editorValue.setSelected(checkBox.isSelected());
            return editorValue;
        }

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

            System.out.println("...");

            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                State state = (State) node.getUserObject();
                editorValue = state;
                checkBox.setText(state.getText());
                checkBox.setSelected(state.isSelected());
            } else {
                checkBox.setText("??");
                checkBox.setSelected(false);
            }

            return checkBox;

        }

    }

    public class StateRenderer implements TreeCellRenderer {

        private JCheckBox checkBox;

        public StateRenderer() {
            checkBox = new JCheckBox();
            checkBox.setOpaque(false);
        }

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

            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                State state = (State) node.getUserObject();
                checkBox.setText(state.getText());
                checkBox.setSelected(state.isSelected());
            } else {
                checkBox.setText("??");
                checkBox.setSelected(false);
            }

            if (selected) {
                checkBox.setBackground(UIManager.getColor("Tree.selectionBackground"));
                checkBox.setForeground(UIManager.getColor("Tree.selectionForeground"));
            } else {
                checkBox.setForeground(tree.getForeground());
            }

            checkBox.setOpaque(selected);

            return checkBox;
        }
    }
}
 类似资料:
  • 我想知道如何实现这个功能: 我有一个可编辑的JTree,可以编辑节点的名称。如果我有一个节点是分支节点(其中有一些叶节点),并且该分支节点在编辑时展开,编辑后,该节点将折叠。 编辑完成后,如果分支节点打开,我想让它保持打开状态,如果分支节点折叠,我想让它折叠。 我试图查看TreeWireExpandListener,但它似乎无法解决我的问题,因为在调用这些方法之前,我需要识别实际节点是否处于编辑模

  • 问题内容: 我在创建JTree时遇到困难,该JTree允许通过将节点拖放到JTree中进行重组。看来应该比较简单。我在网上看过示例,但似乎无法在自己的代码中实现它。 例如,sun提供的这种功能允许在不同组件之间拖动到树中,但不能从树本身内部拖动。 而且我也发现了这一点,它允许您将文本拖到JTree中,但不能拖到树中。 任何参考或建议将是巨大的。谢谢 问题答案: 之前没有做过,但是谷歌快速搜索在这里

  • 我想为JTree中的每个节点设置一个不同的图标,实际上我是从数据库中加载每个节点,使用“while”,我将每个图标设置为根、叶或父。这样地: 我的所有声明都是全球性的: 这是我设置节点的代码: makeNode方法如下: 在用我的节点填充treemodel之后,我将模型设置为我的JTree: 但问题是。当我尝试设置图标时。我创建了一个名为myTreeRenler的子类,我使用它: 但它没有设置我想

  • 我在这里使用这个示例代码创建一个JTree,其中所有的叶子都有一个复选框。我遇到的问题是,我还需要能够选择节点对象,而不必勾选复选框。在我的用例中,勾选复选框将使某些内容可见或隐藏,但选择节点将允许在UI的单独部分编辑对象。最好的方法是什么?是否可以检测何时单击复选框的“框”部分,或者是否选中节点? 我看到的另一个错误是,当文件夹有子元素时,文件夹左侧的指示器正确显示文件夹已打开: 但是如果'文件

  • 我使用的是从数据库填充的JTree。 通过将根节点及其子节点设置为自定义对象,可以创建树: 在这之后,树被完美地创造出来。使用类别对象填充,每个对象都有自己的ID号和名称,以便在toString()方法中使用。 当它被设置为可编辑时,问题就来了。一旦节点被重命名,类别节点也被转换成一个字符串对象,所以我不能更新新的类别名称值到数据库。 我试图用捕捉重命名事件,但userObject已更改为Stri

  • 问题内容: 我想为JTree中的每个节点设置一个不同的图标,实际上是从数据库中加载每个节点,并带有“ while”,我将每个图标设置为根,叶或父级。像这样: 我所有的声明都是全球性的: 这是我设置节点的代码: 方法makeNode是这样的: 在用节点填充treemodel之后,将模型设置为JTree: 但是问题是。当我尝试设置图标时。我创建一个名为myTreeRenderer的子类,并使用以下代码