• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to set image as background of JTree component

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic