using Swing's JTree...

Posted on 1998-04-14
Last Modified: 2012-06-21
I am using Swing's JTree.  I want to know if it is possible to set a ToolTip for each node on the tree.  If this is possible, how do I do it?
Question by:stepma
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1234089
TreeNode t = new TreeNode(......)

If this works, make it an answer ;-)


Expert Comment

ID: 1234090
This one is tough. I tried using a custom TreecellRenderer and setting tooltips in its constructor, but I failed to get it to work.

Still trying, though...


Accepted Solution

evijay earned 50 total points
ID: 1234091

For a tree node, you can get the component which renders the
tree  and then set its tooltip


Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

ID: 1234092
Now this will set the tooltip text for each node on the tree.  In other words I want to be able to set the tooltip text for each node to be the text of that node so that if the node's label runs past the viewable area, I don't have to use the horizontal scrollbar, rather just see the tooltip.  Therefore I want each node to have a it's own tooltip text.  Will this work for that?

Author Comment

ID: 1234093
Where does the method getTreeCellRendererComponent(...) come from and do I have to do this for each node?

Author Comment

ID: 1234094
First of all it returns Component, not JComponent, so how can you set the tooltip?  You can't set the tooltip on Component, only JComponent.

Expert Comment

ID: 1234095
If you downloaded the swing package from javasoft, there is an example in SampleTree directory which gives the answer to your
problem. Run the example and see how it solves your problem.
The example uses a custom cellrenderer and has tooltips for each node
The example is in examples\SampleTree directory.
Look for file

If you dont want to go into the business of installing your own tree cell renderer, then get the default tree cell renderer and
set its tooltip.
if you want still more details send a comment

The getTreeCellRendererComponent(...) method

Author Comment

ID: 1234096
getTreeCellRendererComponent(...) returns Component not JComponent, so you can't setToolTipText on java.awt.Component.  You can only do that on thanks.

Expert Comment

ID: 1234097
if you invoke getCellRenderer() on the JTree, you will be returned an object which you can safely cast to JLabel. Then, you can set the tooltip. But note that since you want the tooltip to be set differently for each node, its better you write your custom cell renderer. Otherwise, the solution is a round about one. Extract the inthe swing software and use the code in and modify the getTreeCellRendererComponent to set the tooltip appropriately as in SampleTreeCellRenderer. Compile the thing and put it in your directory path first.

Author Comment

ID: 1234098
ok, I'll see what I can do.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

717 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