Solved

using Swing's JTree...

Posted on 1998-04-14
10
1,124 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
Comment Utility
TreeNode t = new TreeNode(......)
t.setToolTipText('Tooltip');

If this works, make it an answer ;-)

Michel
0
 
LVL 1

Expert Comment

by:dufort
Comment Utility
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
Comment Utility
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
 

Author Comment

by:stepma
Comment Utility
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
Comment Utility
Where does the method getTreeCellRendererComponent(...) come from and do I have to do this for each node?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:stepma
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok, I'll see what I can do.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 37
topping3 challenge 14 48
What is the latest versions eclipse neon 2 113
unix in java example 9 36
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)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

728 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

9 Experts available now in Live!

Get 1:1 Help Now