根据深度级别更改JTree节点图标

发布于 2024-10-11 03:19:55 字数 149 浏览 4 评论 0原文

我正在寻找更改我的 JTree (Swing) 的不同图标

java 文档解释了如何更改节点是否为叶子的图标,但这实际上不是我要搜索的内容。

对我来说,节点是否是叶子并不重要,或者,如果节点位于三个深度级别中的第一/第二/第三深度级别,我只想更改图标。

I'm looking for changing the different icons of my JTree (Swing)

The java documentation explains how to change icons if a node is a leaf or not, but that's really not what I'm searching.

For me it doesn't matter if a node is a leaf or, I just want to change the icons if the node is in the first/2nd/3rd depth level of the three.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-10-18 03:19:55

作为自定义 TreeCellRenderer 的替代方案,您可以替换 collapsedIconexpandedIcon 的 UI 默认值:

Icon expanded = new TreeIcon(true, Color.red);
Icon collapsed = new TreeIcon(false, Color.blue);
UIManager.put("Tree.collapsedIcon", collapsed);
UIManager.put("Tree.expandedIcon", expanded);

TreeIcon 很简单Icon 接口的实现:

class TreeIcon implements Icon {

    private static final int SIZE = 14;
    private boolean expanded;
    private Color color;

    public TreeIcon(boolean expanded, Color color) {
        this.expanded = expanded;
        this.color = color;
    }

    //@Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(color);
        if (expanded) {
            g2d.fillOval(x + SIZE / 4, y, SIZE / 2, SIZE);
        } else {
            g2d.fillOval(x, y + SIZE / 4, SIZE, SIZE / 2);
        }
    }

    //@Override
    public int getIconWidth() {
        return SIZE;
    }

    //@Override
    public int getIconHeight() {
        return SIZE;
    }
}

As an alternative to a custom TreeCellRenderer, you can replace the UI defaults for collapsedIcon and expandedIcon:

Icon expanded = new TreeIcon(true, Color.red);
Icon collapsed = new TreeIcon(false, Color.blue);
UIManager.put("Tree.collapsedIcon", collapsed);
UIManager.put("Tree.expandedIcon", expanded);

TreeIcon is simply an implementation of the Icon interface:

class TreeIcon implements Icon {

    private static final int SIZE = 14;
    private boolean expanded;
    private Color color;

    public TreeIcon(boolean expanded, Color color) {
        this.expanded = expanded;
        this.color = color;
    }

    //@Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(color);
        if (expanded) {
            g2d.fillOval(x + SIZE / 4, y, SIZE / 2, SIZE);
        } else {
            g2d.fillOval(x, y + SIZE / 4, SIZE, SIZE / 2);
        }
    }

    //@Override
    public int getIconWidth() {
        return SIZE;
    }

    //@Override
    public int getIconHeight() {
        return SIZE;
    }
}
留一抹残留的笑 2024-10-18 03:19:55

实现自定义 TreeCellRenderer - 对组件使用 JLabel,并使用存储在树中的对象数据设置其图标。如果对象是原始对象(例如字符串),您可能需要包装对象以存储其深度等

http://download.oracle.com/javase/7/docs/api/javax/swing/tree/TreeCellRenderer.html
http://www.java2s.com/Code/Java/Swing-JFC /TreeCellRenderer.htm

Implement a custom TreeCellRenderer - use a JLabel for the component, and set its icon however you like using the data of the Object stored in the tree. You may need to wrap the object to store its depth, etc. if the object is primitive (String for example)

http://download.oracle.com/javase/7/docs/api/javax/swing/tree/TreeCellRenderer.html
http://www.java2s.com/Code/Java/Swing-JFC/TreeCellRenderer.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文