• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!