I want to have a clickable link in a Jlabel tooltip. As this isn't supported out of the box, I've found this solution, which I've adapted to fit my needs (see code underneath).
With normal tooltips, I got used to use parapraphs to automatically wrap long tooltips to a given width (e.g. <html><p width="300">Setting 'File' will show a single video which will be played with the first subtitle found for the specified languages.<br><br>Using 'Folder' will show a folder containing a list of videos with different subtitles.</p></html>
).
Now when I use the paragraph with the HyperLinkToolTip, there is a margin on top I can't figure out how to remove. I've tried playing with margin and padding but it didn't work.
package test;
import java.awt.Desktop;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ToolTipUI;
public class HyperLinkToolTip extends JToolTip {
private static final long serialVersionUID = -8107203112982951774L;
private JEditorPane theEditorPane;
public HyperLinkToolTip() {
setLayout(new GridLayout());
theEditorPane = new JEditorPane();
theEditorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
theEditorPane.setContentType("text/html");
theEditorPane.setEditable(false);
theEditorPane.setForeground(new ColorUIResource(255, 255, 255));
theEditorPane.setBackground(new ColorUIResource(125, 184, 47));
theEditorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported())
{
try {
Desktop.getDesktop().browse(new URI(e.getDescription()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
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() {
private static final long serialVersionUID = -2927951764552780686L;
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><p width=\"300\">Specifies the languages to search for subtitles. E.g. 'eng', 'eng,fra,esp'.<br><br>See <a href=\"http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes\">this article</a> for a full list of languages.</p></html>");
panel.add(btn);
frame.setContentPane(panel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setSize(400, 400);
frame.setVisible(true);
}
});
}
}
The result looks like this:
Question: Can anyone tell me how to avoid the gap on the top of the tooltip?
It looks like you can use <div>...</div>
tags instead of the <p>...</p> tags
.
btn.setToolTipText("<html><div width=\"300\">Specifies the languages to search for subtitles. E.g. 'eng', 'eng,fra,esp'.<br><br>See <a href=\"http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes\">this article</a> for a full list of languages.</div></html>");