• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set image as background of JTree component

 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

i was involved in developing a swing project. In that a need to set background of JTree with a custom image. i had done some coding for that, but this code draws image onto the tree not as background to tree.


My Question is how to set image as background of JTree.
Please help me

sasikala
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I would do it is put the image on the container (JPanel) that the JTree is a child of. Then set the opageness of the JTree to false ( tree.setOpaque(false) ). This will give you the desired affect.

Another option *might* be to sublcass JTree and override it's paintComponent method and draw the image there instead of on the container. However, drawing it on the container is better, IMO.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line

will load the image again each time the paint code executes. To see how often this can happen add a marker, like System.out.println("yo");, in the paint method and hide/show, minimize/restore or partially cover and uncover the top–level container. We only need to load it once. I would move the line to a class constructor.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good solution Craig. In my suggested solution, I didn't take into account putting the JTree on a JScrollPane which would make my solution more difficult.
 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Craig Wood & Gregg Bolinger,

Thank you very much. it is working, i couldn't believe this after seeing the result also. setting multi color and image both are working.

once again thank you very much.

Sasikala.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!