0

I have some custom JTree. That tree has nodes with custom icons. I also have a class that extends DefaultTreeCellRenderer with method getTreeCellRendererComponent as simple as this:

super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
MyTreeNode node = ((MyTreeNode) value);
Icon icon = node.getIcon();
setIcon(icon);
return this;

It works. My tree nodes will be rendered with custom icons in ICON TEXT format. BUT! Is there a way to put TWO icons in my node! I would like to have a node that looks like this: ICON TEXT ANOTHER_JLABEL_WITH_IMAGE.

How to do it?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
guest86
  • 2,894
  • 8
  • 49
  • 72

4 Answers4

2

You should create your own TreeCellRenderer that extends from something like a JPanel.

This way, you can define your own layout requirements and add your own components to support your requirements

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

You can also create an Icon which combines two (or more) other icons. See for example http://tips4java.wordpress.com/2009/03/29/compound-icon/

Walter Laan
  • 2,956
  • 17
  • 15
1

The default component used to render a Tree node is a JLabel. Thus you have access to the setIcon method.

But if you want to set two icons to a node you need to create a JPanel with two labels in it.

mkhelif
  • 1,551
  • 10
  • 18
1
  • (J)Component / JLabel (returned by Renderer) haven't implemented any LayoutManager in the API

  • right JLabel has constructor for Text and Icon, there is not place for another Icon

  • basically every JComponents are containers too, part of Containers have got implemented LayoutManager in the API (JFrame, JPanel), you can to put any JComponent to the another

  • for example

  • great workarounds by camickr, especially Compound Icon

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319