How to remove folder symbol which comes in front of each node from JTree in java

后端 未结 3 664
抹茶落季
抹茶落季 2020-12-11 07:43

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

相关标签:
3条回答
  • 2020-12-11 08:15
    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;
                    }
                });
    
    0 讨论(0)
  • 2020-12-11 08:21

    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.

    0 讨论(0)
  • 2020-12-11 08:33

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题