I want to add some tooltips to only a certain text inside a JTextPane. As an example, if there is a reference link text inside the JTextPane I want to add a tooltip to that text to show the link. Is there any way I can achieve this functionality?
Use the setToolTipText method to set up a tool tip for the component. For example, to add tool tips to three buttons, you add only three lines of code: b1. setToolTipText("Click this button to disable the middle button."); b2.
Basic Tooltip HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .
Tooltips are usually initiated in one of two ways: through a mouse-hover gesture or through a keyboard-hover gesture. (In case you're wondering what keyboard hover is: to access the active elements on a page, users can usually move the mouse to them or tab through them using the keyboard.
We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s.
Good question.
First Swing supports HTML, so to show tooltip with link you just have to say:
comp.setToolTipText("<html><a href='http://www.google.com'>google</a></html>");
The problem is making this tooltip clickable.
Unfortunately it is not done by Swing itself.
Tooltip is created by ToolTipManager. When you call setToolTipText() Jcomponent adds the instance of itself to shared instance of Tooltip manager that is responsible on showing the tooltip (using method show()
that cannot be overridden. You cannot change the tooltip manager itself too.
So, the best solution I can suggest is to do the following.
You can listen to the AWT events using Toolkit.getDefaultToolkit().addAWTEventListener()
So, when tooltip is being showed catch it, discover, and add mouse listener on it. This mouse listener will make the tooltip itself clickable.
Here is the exercise I have just written. You can use it as a reference. Good luck.
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
final JFrame f = new JFrame("test");
f.setSize(100, 100);
JLabel l = new JLabel("<html><a href='http://www.google.com'>google</a></html>");
l.setToolTipText("<html><a href='http://www.google.com'>google</a></html>");
long mask = AWTEvent.COMPONENT_EVENT_MASK |
// AWTEvent.CONTAINER_EVENT_MASK |
// AWTEvent.FOCUS_EVENT_MASK |
// AWTEvent.KEY_EVENT_MASK |
// AWTEvent.MOUSE_EVENT_MASK |
// AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.WINDOW_EVENT_MASK |
AWTEvent.ACTION_EVENT_MASK |
AWTEvent.ADJUSTMENT_EVENT_MASK |
AWTEvent.ITEM_EVENT_MASK |
AWTEvent.TEXT_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
int id = event.getID();
Object source = event.getSource();
if (id == 101 && source instanceof JToolTip) {
JToolTip tooltip = (JToolTip)source;
//System.out.println("" + event.getID() + " " + event.getSource());
}
}
}, mask);
f.add(l);
f.setVisible(true);
}
Override: getToolTipText(MouseEvent event) method of the text pane.
Using the MouseEvent you can use the viewToModel(...) method to get the offest into the Document. Then you can get the attributes to determine if you are hovering over a link.
Or maybe an easier approach is to use the getCursor() method. When the cursor is the hand cursor you are over a link.
Then you can return the appropriate tool tip text for the current link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With