Java JTree 自定义图标在编辑树单元格时变回默认图标

Java JTree custom icon changes back to default icon while editing tree cell

在我的代码中,我自定义了一个 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;
            }
        });

在我将我的树设置为可编辑以便用户可以编辑节点标识符之前,所有这些都工作得很好。现在,在编辑节点时图标会切换回默认值。好像编辑树细胞渲染器和我写的不一样。有谁知道如何解决这个问题?

使用 DefaultTreeCellRenderersetLeafIcon(Icon)setOpenIcon(Icon)setClosedIcon(Icon) 方法可能更简单。

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

public class TreeIconTest {
  private final Icon icon = new Icon() {
    @Override public void paintIcon(Component c, Graphics g, int x, int y) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.translate(x, y);
      g2.setPaint(Color.RED);
      g2.fillRect(1, 1, getIconWidth() - 2, getIconHeight() - 2);
      g2.dispose();
    }
    @Override public int getIconWidth() {
      return 16;
    }
    @Override public int getIconHeight() {
      return 16;
    }
  };
  public JComponent makeUI() {
    JTree tree1 = new JTree();
    tree1.setEditable(true);
    tree1.setCellRenderer(new DefaultTreeCellRenderer() {
      @Override
      public Component getTreeCellRendererComponent(
          JTree tree, Object value, boolean selected, boolean expanded,
          boolean isLeaf, int row, boolean focused) {
        JLabel c = (JLabel) super.getTreeCellRendererComponent(
            tree, value, selected, expanded, isLeaf, row, focused);
        c.setIcon(icon);
        return c;
      }
    });

    JTree tree2 = new JTree();
    tree2.setEditable(true);
    DefaultTreeCellRenderer renderer2 = new DefaultTreeCellRenderer();
    renderer2.setOpenIcon(icon);
    renderer2.setClosedIcon(icon);
    renderer2.setLeafIcon(icon);
    tree2.setCellRenderer(renderer2);

    JTree tree3 = new JTree();
    tree3.setEditable(true);
    tree3.setCellRenderer(new DefaultTreeCellRenderer() {
      @Override public Icon getOpenIcon() {
        return icon;
      }
      @Override public Icon getClosedIcon() {
        return icon;
      }
      @Override public Icon getLeafIcon() {
        return icon;
      }
    });
    tree3.setCellEditor(new DefaultTreeCellEditor(
        tree3, (DefaultTreeCellRenderer) tree3.getCellRenderer()));

    JPanel p = new JPanel(new GridLayout(1, 3));
    p.add(new JScrollPane(tree1));
    p.add(new JScrollPane(tree2));
    p.add(new JScrollPane(tree3));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new TreeIconTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}