Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to set image as background of JTree component  RSS feed

 
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
 
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.
 
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!