aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding an image to a JTree node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding an image to a JTree node" Watch "Adding an image to a JTree node" New topic
Author

Adding an image to a JTree node

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Check out http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

You mean when the node is selected (by the user clicking on it)?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

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

Joined: Aug 31, 2004
Posts: 225
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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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

Joined: Aug 31, 2004
Posts: 225
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
 
subject: Adding an image to a JTree node