JTree TreeCellRenderer raising issue on showing the selection color
Asked Answered
S

2

3

I am using this below piece of code:

 class CountryTreeCellRenderer implements TreeCellRenderer {
        private JLabel label;

        CountryTreeCellRenderer() {
            label = new JLabel();
        }

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Object o = ((DefaultMutableTreeNode) value).getUserObject();
            if (o instanceof Country) {
                Country country = (Country) o;
                label.setIcon(new ImageIcon(country.getFlagIcon()));
                label.setText(country.getName());
            } else {
                label.setIcon(null);
                label.setText("" + value);
            }
            return label;
        }
    }

Since I am passing/returning a label, so on selecting any component in the JTree, no selection color is coming. I tried to use:

JComponent comp = (JComponent) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
comp.setOpaque(true);
if(selected)
comp.setBackground(Color.RED);

But if I return comp, then the output of the tree is not coming as expected.

How to resolve the same?

I did not implement any Editor for the same.

Saddlebacked answered 29/1, 2012 at 6:43 Comment(3)
For better help sooner, post an SSCCE.Reconstitute
+1 for not extending a component :-) It's the job of the renderer to configure the rendering component's visual properties (like, font and colors) based on the parameters passed-in into the getXXRenderingComponentAdministrator
Also, #16500914Rehabilitation
N
3

Please take a look at the source code of the DefaultTreeCellRenderer, which extends JLabel as well and is perfectly capable of setting a background color. I copy-pasted the relevant lines below:

  if (selected)
    {
      super.setBackground(getBackgroundSelectionColor());
      setForeground(getTextSelectionColor());

      if (hasFocus)
        setBorderSelectionColor(UIManager.getLookAndFeelDefaults().
                                getColor("Tree.selectionBorderColor"));
      else
        setBorderSelectionColor(null);
    }
  else
    {
      super.setBackground(getBackgroundNonSelectionColor());
      setForeground(getTextNonSelectionColor());
      setBorderSelectionColor(null);
    }
Nemhauser answered 29/1, 2012 at 9:20 Comment(2)
-1 a) extending a component is dirty design b) mixing calls to super and this is calling for pain (f.i. the infamous color memory in the default table cell renderer)Administrator
@Administrator I should have made my answer more clear. I wanted to illustrate that it is perfectly possible to obtain the desired behavior by using a JLabel, and used the DefaultTreeCellRenderer as example since it saved me the effort of writing some code. Now that I read my answer again I see it can be misinterpretedNemhauser
S
0

Yes it worked as explained by Robin by bit of change basically

if(selected){
            label.setBackground(Color.YELLOW);
            label.setForeground(Color.GREEN);
        }else
             {
            label.setBackground(Color.WHITE);
            label.setForeground(Color.BLACK);
             //setBorderSelectionColor(null);
             }

enough

Saddlebacked answered 1/2, 2012 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.