I want To insert hyperlink in JPanel (so that on clicking i go to web address) Is it possible ? If yes, how can i do this?
Asked
Active
Viewed 4,230 times
4 Answers
3
See the How to Use HTML in Swing Components tutorial.

stacker
- 68,052
- 28
- 140
- 210
-
@ stacker i want that when i click on hyperlink,i go to the mentioned address.That is not explained there – Suhail Gupta Apr 18 '11 at 14:53
1
I wrote a simple function that takes the text, link, and the position you want to display and returns a JLabel
. When the mouse is over the text, it becomes blue with underline and makes cursor a pointing hand, otherwise it becomes black with no underline and default cursor.
public static JLabel makeHyperLink(final String s, final String link, int x, int y)
{
final JLabel l = new JLabel(s);
l.addMouseListener(new MouseAdapter()
{
@Override
public void mouseExited(MouseEvent arg0)
{
l.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
l.setText(s);
}
@Override
public void mouseEntered(MouseEvent arg0)
{
l.setCursor(new Cursor(Cursor.HAND_CURSOR));
l.setText(String.format("<HTML><FONT color = \"#000099\"><U>%s</U></FONT></HTML>", s));
}
@Override
public void mouseClicked(MouseEvent arg0)
{
try
{
URI uri = new URI(link);
if (Desktop.isDesktopSupported())
Desktop.getDesktop().browse(uri);
} catch (Exception e)
{
}
}
});
l.setBounds(x, y, s.length()*5, 20);
l.setToolTipText(String.format("go to %s", link));
return l;
}
If any part of code is not clear, contact me;)

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

Babak
- 188
- 12
1
We've been using something like this:
public class UrlTextPane extends JTextPane {
private final Pattern urlPattern = Pattern.compile(UrlUtil.URL_REGEX);
public UrlTextPane() {
this.setEditable(false);
this.addHyperlinkListener(new UrlHyperlinkListener());
this.setContentType("text/html");
}
private class UrlHyperlinkListener implements HyperlinkListener {
@Override
public void hyperlinkUpdate(final HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
Desktop.getDesktop().browse(event.getURL().toURI());
} catch (final IOException e) {
throw new RuntimeException("Can't open URL", e);
} catch (final URISyntaxException e) {
throw new RuntimeException("Can't open URL", e);
}
}
}
};
@Override
/**
* Set the text, first translate it into HTML:
*/
public void setText(final String input) {
final StringBuilder answer = new StringBuilder();
answer.append("<html><body style=\"font-size: 8.5px;font-family: Tahoma, sans-serif\">");
final String content = StringEscapeUtils.escapeHtml(input);
int lastIndex = 0;
final Matcher matcher = urlPattern.matcher(content);
while(matcher.find()) {
//Append everything since last update to the url:
answer.append(content.substring(lastIndex, matcher.start()));
final String url = content.substring(matcher.start(), matcher.end()).trim();
if(UrlUtil.isValidURI(url)) {
answer.append("<a href=\"" + url + "\">"+url+"</a>");
} else {
answer.append(url);
}
lastIndex = matcher.end();
}
//Append end:
answer.append(content.substring(lastIndex));
answer.append("</body></html>");
super.setText(answer.toString().replace("\n", "<br />"));
}
}

Roy van Rijn
- 840
- 7
- 17
-
UrlUtil contains an URL regex and a method that tries to parse the URL to a Java URI (which is very strict). – Roy van Rijn Apr 18 '11 at 13:35
0