Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

More CLICKABLE link on a tooltip - how to?

843806Jun 13 2007 — edited Apr 18 2008
One of previous message shows a way of doing clickable link on tooltip. see:
http://forum.java.sun.com/thread.jspa?threadID=592163&messageID=3093997

I modified the code a little bit to put tooltip on a button instead of panel.

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ToolTipUI;

public class HyperLinkToolTip extends JToolTip {
private JEditorPane theEditorPane;

public HyperLinkToolTip() {
setLayout(new BorderLayout());
LookAndFeel.installBorder(this, "ToolTip.border");
LookAndFeel.installColors(this, "ToolTip.background", "ToolTip.foreground");
theEditorPane = new JEditorPane();
theEditorPane.setContentType("text/html");
theEditorPane.setEditable(false);

theEditorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
// do whatever you want with the url
System.out.println("clicked on link : " + e.getDescription());
}
}
});
add(theEditorPane);
}

public void setTipText(String tipText) {
theEditorPane.setText(tipText);
}

public void updateUI() {
setUI(new ToolTipUI() {});
}

public static void main(String[] args) {
final JFrame frame = new JFrame(HyperLinkToolTip.class.getName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();

JButton btn = new JButton() {
public JToolTip createToolTip() {
JToolTip tip = new HyperLinkToolTip();
tip.setComponent(this);
return tip;
}

// Set tooltip location
public Point getToolTipLocation(MouseEvent event) {
return new Point(getWidth()/2, getHeight()/2);
}
};

btn.setText("Tooltip Test");
btn.setToolTipText("<html><body><a href=\"http://www.sun.com\">Goto www.sun.com for details.</a></body></html>");

panel.add(btn);
frame.setContentPane(panel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setSize(400, 400);
frame.setVisible(true);
}
});
}
}

The code works fine in Java application. But, when I use it in an applet, it is almost impossible to click on the tooltip. The tooltip is flashing or disappears when trying to move mouse on the tooltip. I did set the location of tooltip to make sure there is no gap between button and tooltip. Here is the code for applet:

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;

public class ToolTipApplet extends JApplet {
private JPanel jContentPane = null;
private JButton btnTest = null;

public ToolTipApplet() {
super();
}

public void init() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new FlowLayout());
jContentPane.add(getBtnTest(), null);
}
return jContentPane;
}

private JButton getBtnTest() {
if (btnTest == null) {
btnTest = new JButton() {
public JToolTip createToolTip() {
JToolTip tip = new HyperLinkToolTip();
tip.setComponent(this);
return tip;
}

// Set tooltip location
public Point getToolTipLocation(MouseEvent event) {
return new Point(getWidth()/2, getHeight()/2);
}
};

btnTest.setText("Tooltip Test");
btnTest.setToolTipText("<html><body><a href=\"http://www.sun.com\">Goto www.sun.com for details.</a></body></html>");
}
return btnTest;
}
}

No idea why application and applet behave differently here. (I am using JRE1.6u1 for the test.)

Thanks a lot!

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 16 2008
Added on Jun 13 2007
7 comments
683 views