Solved

how do I set tooltip text of a treenode?

Posted on 2001-08-02
12
495 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now