tags:

views:

33

answers:

1

I have a Tree node. I want an image to be displayed beside the node name when the tree is viewed. How do I go about?

A: 
  1. create an ImageIcon from your image

    ImageIcon icon = ....; (there are a number of ways to do this)

  2. create an instance of DefaultTreeCellRenderer

    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();

  3. set the leaf icon field of the renderer

    renderer.setLeafIcon(icon);

  4. set the tree's renderer

    tree.setCellRenderer(renderer);

if you can't figure it out try the demo

pstanton
to add to the above answer:If you are looking to set an image on the right side of the text (basically, not an icon) you still need to create your own instance of DefaultTreeCellRenderer to return a component which includes the image
Thimmayya