Displaying Element-Level Tool Tips for Swing Components
Tool tips are those little popup windows that appear when you rest your mouse pointer over a control such as a button or list. All Swing components support tool tips. To establish a tool tip for a control, call setToolTipText () for the component. When a user rests the mouse pointer over the control, the tool tip automatically appears. Typically, you want one text string for an entire component. But there can be times when you want the text to be more flexible. for instance, in the case of A Jlist, Jtree, or Jtable Component, You Might Want To Display Tool Tip text based on Which List entry, Tree Node, or table Cell the mouse pointer is over. How can you customize the text?
There are two ways you can create custom tool tips for these Swing components. You can customize the control and override the public String getToolTipText (MouseEvent event) method, or you can customize the renderer for the control and let the renderer deal with the tool tips .
If you decide to override the getToolTipText () method, you have to understand that the passed-in MouseEvent contains the mouse coordinates over the control. It is your responsibility to determine what element the cursor is on. Then, you have to look up the appropriate tool tip text for the element and return that text. you must then manually register the component with the ToolTipManager. If you called the setToolTipText () method for the control, a call to the registerComponent () method of ToolTipManager would have been done for You, But Because you don't need to call settooltiptext (), You Must do the registration yourself.
Here's an example that demonstrates this first approach. The example uses the keys for the system properties (such as java.runtime.name) as the list entries. The example uses the property values (such as Java 2, Runtime Environment, Standard Edition) As the Tool Tip text for those entries.import java.awt. *;
Import java.awt.event. *;
Import java.util. *;
Import javax.swing. *;
Public class listtest extends jlist {
DEFAULTLISTMODEL MODEL;
Properties Tipprops;
Public ListTest (Properties Props) {
Model = new defaultListModel ();
SetModel;
Tooltipmanager.sharedInstance (). Registercomponent
THIS);
Tipprops = prOPs;
Enumeration enum = props.propertynames ();
While (enum.hasmoreElements ()) {
MODEL.ADDELEMENT (Enum.nexTelement ());
}
}
Public String GetTooltiptext (MouseEvent Event) {
Point P = Event.getPoint ();
INT location = locationToIndex (P);
String key = (string) Model.getlementat (
Location;
String tip = tipprops.getproperty (key);
Return TIP;
}
Public static void main (string args []) {
JFrame Frame = New Jframe ("Custom Tips");
Frame.setDefaultCloseOperation
JFrame.exit_on_close);
Properties PROPS = system.getproperties ();
ListTest List = New ListTest (PROPS);
JscrollPane scrollpane = new jscrollpane (list);
Frame.getContentPane (). Add (scrollpane);
Frame.setsize (300, 300);
Frame.show ();
}
}
If you decide instead to customize the cell renderer, you have to do a little more work at first, but you do not have to determine what element is under the mouse at run time. If you find that the tool tips are hardly used, though, this extra work might not be necessary. That's because getting cell renderers happens frequently, and the tool tip setup happens every time the cell is rendered. It might be better to use the first mechanism. Essentially, what it involves is calling the setToolTipText () method for the cell renderer.Here's an example that demonstrates the cell renderer approach. The example is taken from the book "John Zukowski's Definitive Guide to Swing for Java 2, Second Edition" published by Apress.
First The Rendrer: This Code Essentially Reuses a DefaultTreecellrenderer as The Renderer, Setting ITS Tip text.
Import javax.swing. *;
Import javax.swing.tree. *;
Import java.awt. *;
Import java.util. *;
Public Class TooltreeCellrenderer
Implements treecellrenderer {
DefaultTreecellRenderer renderer =
New defaulttreecellrenderer ();
Dictionary tiptable;
Public tooltiptreecellrenderer
Dictionary tiptable) {
THIS.TIPTABLE = TipTable;
}
Public Component Gettreecellrenderercomponent
JTree Tree, Object Value, Boolean SELECTED,
Boolean Expanded, Boolean Leaf, Int Row,
Boolean Hasfocus) {
RENDERER.GETTREECELLRENDERERComponent
Tree, Value, Selected, Expanded, Leaf, Row,
Hasfocus;
IF (value! = null) {
Object Tipkey;
IF
Value instanceof defaultmutable accessreenode) {
Tipkey =
(Defaultmutabletreenode) value). GetuserObject ();
} else {
Tipkey = tree.convertValuetotext (Value,
SELECTED, Expanded, Leaf, Row, Hasfocus;
}
Object tip = tiptable.get (tipkey); if (tip! = Null) {
Renderer.SetTooltiptext (Tip.toToString ());
} else {
Renderer.SetTooltiptext (NULL);
}
}
Return renderer;
}
}
The test program then just registers the tree with the ToolTipManager and registers the renderer. There is no need to subclass JTree. Like the JList example, the system properties are used for the component elements. In this case, the tree is only one level deep .
Import javax.swing. *;
Import javax.swing.tree. *;
Import java.awt. *;
Import java.util. *;
Public class treetips {
Public static void main (string args []) {
JFrame Frame = New JFrame ("Custom Tree Tips");
Frame.setDefaultCloseOperation
JFrame.exit_on_close);
Properties PROPS = system.getproperties ();
JTree Tree = New JTREE (PROPS);
Tooltipmanager.sharedInstance (). Registercomponent
Tree);
TreecellRenderer Renderer =
New Tooltreecellrenderer (PrOPS);
Tree.setCellrenderer (rendere);
JscrollPane scrollpane = new jscrollpane (TREE);
Frame.getContentPane (). Add (
Scrollpane, BorderLayout.center;
Frame.setsize (300, 150);
Frame.setVisible (TRUE);
}
}