编辑树单元格时,Java JTree自定义图标会更改回默认图标

时间:2016-08-17 10:20:15

标签: java swing icons jtree treecellrenderer

在我的代码中,我自定义了DefaultTreeCellRenderer,为我的节点提供了一个自定义图标。

setCellRenderer(new DefaultTreeCellRenderer() {
            private static final long serialVersionUID = -2839238218110688876L;
            private ImageIcon icon = myIcon;

            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean focused) {
                super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, hasFocus);
                Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
                setIcon(icon);
                return c;
            }
        });

所有这些工作都很好,直到我使树可编辑,以便用户可以编辑节点标识符。现在,图标在编辑节点时切换回默认值。编辑树单元格渲染器似乎与我编写的不同。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)