• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1180
  • Last Modified:

using Swing's JTree...

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?
0
stepma
Asked:
stepma
1 Solution
 
Michel PlungjanIT ExpertCommented:
TreeNode t = new TreeNode(......)
t.setToolTipText('Tooltip');

If this works, make it an answer ;-)

Michel
0
 
dufortCommented:
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...

0
 
evijayCommented:
ToolTipManager.sharedInstance().registerComponent(tree);

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

getTreeCellRendererComponent(...).setToolTipText("Tooltip");



0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
stepmaAuthor Commented:
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?
0
 
stepmaAuthor Commented:
Where does the method getTreeCellRendererComponent(...) come from and do I have to do this for each node?
0
 
stepmaAuthor Commented:
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.
0
 
evijayCommented:
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 SampleTreeCellRenderer.java

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
0
 
stepmaAuthor Commented:
getTreeCellRendererComponent(...) returns Component not JComponent, so you can't setToolTipText on java.awt.Component.  You can only do that on com.sun.java.swing.JComponent...but thanks.
0
 
evijayCommented:
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 src.zip inthe swing software and use the code in  
com.sun.java.swing.plaf.BasicTreeCellRenderer and modify the getTreeCellRendererComponent to set the tooltip appropriately as in SampleTreeCellRenderer. Compile the thing and put it in your directory path first.
0
 
stepmaAuthor Commented:
ok, I'll see what I can do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now