Solved

using Swing's JTree...

Posted on 1998-04-14
10
1,146 Views
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?
0
Comment
Question by:stepma
10 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1234089
TreeNode t = new TreeNode(......)
t.setToolTipText('Tooltip');

If this works, make it an answer ;-)

Michel
0
 
LVL 1

Expert Comment

by:dufort
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...

0
 
LVL 4

Accepted Solution

by:
evijay earned 50 total points
ID: 1234091
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
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.

 

Author Comment

by:stepma
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?
0
 

Author Comment

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

Author Comment

by:stepma
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.
0
 
LVL 4

Expert Comment

by:evijay
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 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
 

Author Comment

by:stepma
ID: 1234096
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
 
LVL 4

Expert Comment

by:evijay
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 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
 

Author Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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