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

后端 未结 3 672
抹茶落季
抹茶落季 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: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);
        }
    }
    

提交回复
热议问题