How to Add Hyperlink in Jlabel

How to add hyperlink in JLabel?

You can do this using a JLabel, but an alternative would be to style a JButton. That way, you don't have to worry about accessibility and can just fire events using an ActionListener.

  public static void main(String[] args) throws URISyntaxException {
final URI uri = new URI("http://java.sun.com");
class OpenUrlAction implements ActionListener {
@Override public void actionPerformed(ActionEvent e) {
open(uri);
}
}
JFrame frame = new JFrame("Links");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 400);
Container container = frame.getContentPane();
container.setLayout(new GridBagLayout());
JButton button = new JButton();
button.setText("<HTML>Click the <FONT color=\"#000099\"><U>link</U></FONT>"
+ " to go to the Java website.</HTML>");
button.setHorizontalAlignment(SwingConstants.LEFT);
button.setBorderPainted(false);
button.setOpaque(false);
button.setBackground(Color.WHITE);
button.setToolTipText(uri.toString());
button.addActionListener(new OpenUrlAction());
container.add(button);
frame.setVisible(true);
}

private static void open(URI uri) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) { /* TODO: error handling */ }
} else { /* TODO: error handling */ }
}

JLabel hyperlink to open browser at correct URL

Found the problem: on Ubuntu 12.10 I have installed "libgnome2" and it is working perfectly now.

Hyperlinks in JLabels

Swing is not a fully functioned browser. It supports simple HTML including links but do not change the cursor style automatically. As far as I know you have to do it programmatically, i.e. add mouse listener to your label and change the cursor style when your mouse is over your label. Obviously you can write your own class LinkLabel that implements this logic and then use it every time you need.

using JLabel as link to open popup

I personally would recommend the use of a JEditorPane instead of a JPanel; it is much more useful for displaying paragraphs, and can display HTML, such as links. You can then simply call addHyperlinkListener(Some hyperlinklistener) to add a listener, which will be called upon someone clicking the link. You can just pop something up, or maybe open whatever was clicked in a real browser, its up to you.

Here's some example code (haven't test it yet, should work):

JEditorPane ep = new JEditorPane("text/html", "Some HTML code will go here. You can have <a href=\"do1\">links</a> in it. Or other <a href=\"do2\">links</a>.");
ep.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent arg0) {
String data = arg0.getDescription();
if(data.equals("do1")) {
//do something here
}
if(data.equals("do2")) {
//do something else here
}
}
});

How to put an ImageIcon in a JLabel that contains a hyperlink?

For this you have to add mouse listener in JLabel label which contains ImageIcon means your image.

Here is the Code :

    Icon i1 = new ImageIcon(getClass().getResource("image.jpg"));
l4 = new JLabel(i1);

l4.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {

String command = "rundll32 url.dll,FileProtocolHandler https://www.google.com";
try {
Process p = Runtime.getRuntime().exec(command);

} catch (Exception e1) {

JOptionPane.showMessageDialog(null, "\n Error! \n");

}

}
});
add(l4);

I want To insert hyperlink in JPanel

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 />"));
}

}



Related Topics



Leave a reply



Submit