0

I'll be brief; I'd like to change the color of a JTree from the Metal LF's default blue to a gray. I've already looked at a ExtendedJTreeCellRenderer to no avail. Can this be done with UIManager, if not can it be done without having to custom icon the tree? I have 10+ years with Java, thanks.

Screenshot

        public static class ExtendedJTreeCellRenderer extends DefaultTreeCellRenderer
        {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus)
            {
                JComponent c = (JComponent) super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, hasFocus);

                Graphics graphics = c.getGraphics();

                if(graphics!=null) graphics.setColor(Color.GRAY);

                c.setForeground(new Color(140,140,140));

                c.setBackground(new Color(8,8,8));

                c.setFont(new Font("Georgia", Font.PLAIN, 12));

                c.setOpaque(true);

                //

                this.setClosedIcon(new ImageIcon("images\\folder001.png"));

                //this.setOpenIcon(new ImageIcon(""));

                //this.setLeafIcon(new ImageIcon(""));

                //

                return c;
            }
        }
  • Something like [this](https://stackoverflow.com/questions/14563433/jtree-set-background-of-node-to-non-opaque) would probably be better starting point – MadProgrammer May 09 '20 at 02:16
  • Oh, and this -> `Graphics graphics = c.getGraphics();` is a really bad idea, don't do it – MadProgrammer May 09 '20 at 02:17
  • The Java Closed, Open, Leaf icons themselves are blue; we need them gray. Can it be done with UIManager or some other? See the screenshot please. I had a look at the link you provided. – M. Rupplin May 09 '20 at 02:28
  • You can't "change" the color the icons, but you can replace them with your own - [for example](https://stackoverflow.com/questions/20691946/set-icon-to-each-node-in-jtree) - this will effect the default icons, so beware – MadProgrammer May 09 '20 at 02:31
  • See [*How to turn ImageIcon to gray on Swing*](https://stackoverflow.com/q/14358499/230513); more [here](https://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bswing%5d%20tree%20icon). – trashgod May 09 '20 at 02:33
  • Did you try: `UIManager.getLookAndFeelDefaults().put("Tree.background", Color.GRAY);` ? – Abra May 09 '20 at 02:54
  • The ColorConvertOp leaves the background of the image (UIManager.getIcon()) white whereas the GrayFilter does not. Thanks, not bad ideas. Abra, did you get that to work? – M. Rupplin May 10 '20 at 23:28

0 Answers0