• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding an image to a JTree node

 
Andy Hahn
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to add an image and text to a JTree node, and when the image is clicked, simply log a line of text to the console.

I have been researching this one for a while but cannot figure it out. I am also a newbie to swing development Any ideas would be greatly appreciated.

Thanks
 
Andy Hahn
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have actually read that link about a hundred times. The part I cannot figure out is how to register a listener to fire when the icon is clicked. Any ideas?
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean when the node is selected (by the user clicking on it)?
 
Maneesh Godbole
Saloon Keeper
Posts: 10976
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Hahn wrote:..The part I cannot figure out is how to register a listener to fire when the icon is clicked. Any ideas?

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#select

Your requirements are a bit unclear to me. In your original post you mentioned
add an image and text to a JTree node
. In your previous post you also mentioned
when the icon is clicked
. Are you interested in node selection (if yes, the above link will help you). Or are you interested in finding out if the image was clicked (meaning the mouse click happened over the image area? If it is the latter you will have to figure out the image bounds rectangle and check if the mouse click point lies inside that rectangle.
 
Andy Hahn
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean when the node is selected (by the user clicking on it)?


Nope. When the icon is clicked.

Or are you interested in finding out if the image was clicked (meaning the mouse click happened over the image area?


Yes, this is exactly what I am trying to do!

If it is the latter you will have to figure out the image bounds rectangle and check if the mouse click point lies inside that rectangle.


Is this the route I should go? Can you elaborate a little bit more on how I can do this? Do I register a listener on the JTree, etc?

Thanks
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. You use a MouseListener, or a MouseAdapter. But bear in mind that the tree is already listening for mouse clicks and using them to indicate which node is selected. You can also listen for those same mouse clicks and use them for something else, as per your requirement, but the user may be confused by the departure from the standard GUI style.
 
Andy Hahn
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your ideas. It actually seems to be a common approach and Ive seen it in a couple popular swing apps, too bad those apps dont have the source available. Since this seems to be next to impossible to figure out I may just plan on changing my design.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic