Solved

how do I set tooltip text of a treenode?

Posted on 2001-08-02
12
502 Views
Last Modified: 2012-08-14
Hello,

Does anyone know how to set the tooltip text on the node of a JTree? Initially I'd like the node's tooltip text to display "Hello" when I hover the mouse over that node.



I have this code (but it's not working :(

public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
     System.out.println("inside tree cell renderer");
     javax.swing.JComponent c = (javax.swing.JComponent)super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
     
      c.setToolTipText("hhhhhh");
     return c;
}
0
Comment
Question by:TimB
  • 4
  • 4
  • 4
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6344040
I don't think tool tips are supported for tree cells.
The renderer is only used to 'rubberstamp' the cells so it won't work.
0
 
LVL 1

Author Comment

by:TimB
ID: 6344121
Bugger!

I'll leave the question open for a bit though - just in case someone can come up with something.

Thanks,


Tim
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6344340
Try it :

public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, Object value, boolean
selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        javax.swing.JLabel c = (javax.swing.JLabel)super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
   
    c.setToolTipText("hhhhhh");
    return c;
}


You must add this line as well :

ToolTipManager.sharedInstance().registerComponent(yourJTree);

Best Regards,
Valeri
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:TimB
ID: 6344851
Hi Valeri,

That didn't seem to work either.  I've tried sticking some system.out's in the code so I can see when the rellrenderer get's called.  I'm not missing something silly like a mouse listener for when I hover over ther treenode am I?

Thanks for the reply anyhow,

TimB
0
 
LVL 16

Accepted Solution

by:
Valeri earned 100 total points
ID: 6344928
The proposed code works !!! I'm sure, because I have JTree with ToolTips in my project. There is something wrong in your code, check it again...

Do you have it :
yourJTree.setCellRenderer(new YourRenderer());

Best Regards,
Valeri
0
 
LVL 1

Author Comment

by:TimB
ID: 6345082
Fantastic!

It works! I'd missed out the - ToolTipManager.sharedInstance().registerComponent(yourJTree); - part that you suggested.

Thanks for the help!

TimB
0
 
LVL 1

Author Comment

by:TimB
ID: 6345084
Thanks,

TimB
0
 
LVL 92

Expert Comment

by:objects
ID: 6347885
Cool, I've learnt something today :-)

Does that mean that getTreeCellRendererComponent is called every time the mouse moves over the tree cell?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6348042
Yes, but only if you have registered your JTree in ToolTipManager...

Best Regards,
Valeri
0
 
LVL 92

Expert Comment

by:objects
ID: 6348052
Thanks Valeri :)
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6348075
Hi Objects :-)
I've learned many things reading your answers here...

Thanks !!!
0
 
LVL 92

Expert Comment

by:objects
ID: 6348082
(-:
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
even odd program using while loop 3 60
ejb stateless example 2 25
java example issue 3 23
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question