如何从java中的JTree中删除每个节点前面的文件夹符号

发布于 2024-10-21 08:54:44 字数 47 浏览 2 评论 0原文

我试图从默认情况下的 JTree 节点中删除文件夹符号。 我怎样才能做到这一点?

I am trying to remove the folder symbol from node of JTree which comes by default.
How can I accomplish this?

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

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

发布评论

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

评论(3

ゃ懵逼小萝莉 2024-10-28 08:54:44

仅供参考,这是一个完整的示例:

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;

/** @see http://stackoverflow.com/questions/5260223 */
public class JTreeLite {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            //@Override
            public void run() {
                createGUI();
            }
        });
    }

    private static void createGUI() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Icon empty = new TreeIcon();
        UIManager.put("Tree.closedIcon", empty);
        UIManager.put("Tree.openIcon", empty);
        UIManager.put("Tree.collapsedIcon", empty);
        UIManager.put("Tree.expandedIcon", empty);
        UIManager.put("Tree.leafIcon", empty);

        JTree jt = new JTree();
        frame.add(jt);
        frame.pack();
        frame.setSize(300, 400);
        frame.setVisible(true);
    }
}

class TreeIcon implements Icon {

    private static int SIZE = 0;

    public TreeIcon() {
    }

    public int getIconWidth() {
        return SIZE;
    }

    public int getIconHeight() {
        return SIZE;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        System.out.println(c.getWidth() + " " + c.getHeight() + " " + x + " " + y);
    }
}

Just for reference, here's a complete example:

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;

/** @see http://stackoverflow.com/questions/5260223 */
public class JTreeLite {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            //@Override
            public void run() {
                createGUI();
            }
        });
    }

    private static void createGUI() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Icon empty = new TreeIcon();
        UIManager.put("Tree.closedIcon", empty);
        UIManager.put("Tree.openIcon", empty);
        UIManager.put("Tree.collapsedIcon", empty);
        UIManager.put("Tree.expandedIcon", empty);
        UIManager.put("Tree.leafIcon", empty);

        JTree jt = new JTree();
        frame.add(jt);
        frame.pack();
        frame.setSize(300, 400);
        frame.setVisible(true);
    }
}

class TreeIcon implements Icon {

    private static int SIZE = 0;

    public TreeIcon() {
    }

    public int getIconWidth() {
        return SIZE;
    }

    public int getIconHeight() {
        return SIZE;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        System.out.println(c.getWidth() + " " + c.getHeight() + " " + x + " " + y);
    }
}
软糯酥胸 2024-10-28 08:54:44
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
    renderer.setLeafIcon(null);
    renderer.setClosedIcon(null);
    renderer.setOpenIcon(null);

jtree使用CellRender,例如DefaultTreeCellRenderer,您可以禁用或更改默认节点图标。此外,您还可以创建自定义 cellRender 并为您的图标方案定义更困难的逻辑。

tree.setCellRenderer(new DefaultTreeCellRenderer() {
                @Override
                public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                    JLabel component = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                    ImageIcon iconPath = ((WizardNode) value).getIcon();
                    component.setIcon(iconPath);
                    return this;
                }
            });
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
    renderer.setLeafIcon(null);
    renderer.setClosedIcon(null);
    renderer.setOpenIcon(null);

The jtree uses CellRender, for example DefaultTreeCellRenderer, and you can dasable or change default nodes icon. Also, you can create custom cellRender and defined more dificult logic for you icon scheme.

tree.setCellRenderer(new DefaultTreeCellRenderer() {
                @Override
                public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                    JLabel component = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                    ImageIcon iconPath = ((WizardNode) value).getIcon();
                    component.setIcon(iconPath);
                    return this;
                }
            });
海螺姑娘 2024-10-28 08:54:44

Swing 教程专门讨论了如何执行此操作:

http://download。 oracle.com/javase/tutorial/uiswing/components/tree.html#display

您可以轻松更改默认图标
用于叶子、展开的树枝或
折叠的分支节点。为此,您
首先创建一个实例
默认TreeCell渲染器。

The Swing tutorial talks specifically about doing this:

http://download.oracle.com/javase/tutorial/uiswing/components/tree.html#display

You can easily change the default icon
used for leaf, expanded branch, or
collapsed branch nodes. To do so, you
first create an instance of
DefaultTreeCellRenderer.

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