How to add a hyperlink in JLabel

What is the best way to add a hyperlink to jLabel? I can get an idea using html tags, but how to open a browser when a user clicks on it?

+80
java swing hyperlink jlabel
Feb 09 '09 at 10:58
source share
12 answers

You can do this using JLabel , but the JButton style would be an alternative. This way you don't have to worry about accessibility and can just fire events using the 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 */ } } 
+86
Feb 09 '09 at 12:03
source share

I would like to offer one more solution. It is similar to the one already proposed, because it uses the HTML code in JLabel and registers the MouseListener on it, but it also displays the HandCursor when moving the mouse over the link, so the appearance looks the same as most users would expect. If browsing is not supported by the platform, a blue underlined HTML link is not created, which may mislead the user. Instead, the link is simply presented as plain text. This can be combined with the SwingLink class proposed by @ dimo414.

 public class JLabelLink extends JFrame { private static final String LABEL_TEXT = "For further information visit:"; private static final String A_VALID_LINK = "http://stackoverflow.com"; private static final String A_HREF = "<a href=\""; private static final String HREF_CLOSED = "\">"; private static final String HREF_END = "</a>"; private static final String HTML = "<html>"; private static final String HTML_END = "</html>"; public JLabelLink() { setTitle("HTML link via a JLabel"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel label = new JLabel(LABEL_TEXT); contentPane.add(label); label = new JLabel(A_VALID_LINK); contentPane.add(label); if (isBrowsingSupported()) { makeLinkable(label, new LinkMouseListener()); } pack(); } private static void makeLinkable(JLabel c, MouseListener ml) { assert ml != null; c.setText(htmlIfy(linkIfy(c.getText()))); c.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); c.addMouseListener(ml); } private static boolean isBrowsingSupported() { if (!Desktop.isDesktopSupported()) { return false; } boolean result = false; Desktop desktop = java.awt.Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.BROWSE)) { result = true; } return result; } private static class LinkMouseListener extends MouseAdapter { @Override public void mouseClicked(java.awt.event.MouseEvent evt) { JLabel l = (JLabel) evt.getSource(); try { URI uri = new java.net.URI(JLabelLink.getPlainLink(l.getText())); (new LinkRunner(uri)).execute(); } catch (URISyntaxException use) { throw new AssertionError(use + ": " + l.getText()); //NOI18N } } } private static class LinkRunner extends SwingWorker<Void, Void> { private final URI uri; private LinkRunner(URI u) { if (u == null) { throw new NullPointerException(); } uri = u; } @Override protected Void doInBackground() throws Exception { Desktop desktop = java.awt.Desktop.getDesktop(); desktop.browse(uri); return null; } @Override protected void done() { try { get(); } catch (ExecutionException ee) { handleException(uri, ee); } catch (InterruptedException ie) { handleException(uri, ie); } } private static void handleException(URI u, Exception e) { JOptionPane.showMessageDialog(null, "Sorry, a problem occurred while trying to open this link in your system standard browser.", "A problem occured", JOptionPane.ERROR_MESSAGE); } } private static String getPlainLink(String s) { return s.substring(s.indexOf(A_HREF) + A_HREF.length(), s.indexOf(HREF_CLOSED)); } //WARNING //This method requires that s is a plain string that requires //no further escaping private static String linkIfy(String s) { return A_HREF.concat(s).concat(HREF_CLOSED).concat(s).concat(HREF_END); } //WARNING //This method requires that s is a plain string that requires //no further escaping private static String htmlIfy(String s) { return HTML.concat(s).concat(HTML_END); } /** * @param args the command line arguments */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new JLabelLink().setVisible(true); } }); } } 
+26
Dec 05 2018-11-12T00:
source share

I wrote an article on how to set a hyperlink or mailto in jLabel.

So just try it :

I think that is exactly what you are looking for.

Here is the complete code example:

 /** * Example of a jLabel Hyperlink and a jLabel Mailto */ import java.awt.Cursor; import java.awt.Desktop; import java.awt.EventQueue; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author ibrabelware */ public class JLabelLink extends JFrame { private JPanel pan; private JLabel contact; private JLabel website; /** * Creates new form JLabelLink */ public JLabelLink() { this.setTitle("jLabelLinkExample"); this.setSize(300, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); pan = new JPanel(); contact = new JLabel(); website = new JLabel(); contact.setText("<html> contact : <a href=\"\">YourEmailAddress@gmail.com</a></html>"); contact.setCursor(new Cursor(Cursor.HAND_CURSOR)); website.setText("<html> Website : <a href=\"\">http://www.google.com/</a></html>"); website.setCursor(new Cursor(Cursor.HAND_CURSOR)); pan.add(contact); pan.add(website); this.setContentPane(pan); this.setVisible(true); sendMail(contact); goWebsite(website); } /** * @param args the command line arguments */ public static void main(String args[]) { /* * Create and display the form */ EventQueue.invokeLater(new Runnable() { @Override public void run() { new JLabelLink().setVisible(true); } }); } private void goWebsite(JLabel website) { website.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { try { Desktop.getDesktop().browse(new URI("http://www.google.com/webhp?nomo=1&hl=fr")); } catch (URISyntaxException | IOException ex) { //It looks like there a problem } } }); } private void sendMail(JLabel contact) { contact.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { try { Desktop.getDesktop().mail(new URI("mailto:YourEmailAddress@gmail.com?subject=TEST")); } catch (URISyntaxException | IOException ex) { //It looks like there a problem } } }); } } 
+16
Sep 02 '12 at 12:52
source share

Perhaps instead of JXHyperlink from SwingX . It extends JButton . Some useful links:

+14
Feb 09 '09 at 11:54
source share

Update . I embellished the SwingLink class and added additional features; An updated copy can be found here: https://bitbucket.org/dimo414/jgrep/src/tip/src/grep/SwingLink.java




@McDowell's answer is great, but there are a few things that can be improved. In particular, text other than a hyperlink can be clicked, and it still looks like a button, although some style elements have been changed / hidden. While accessibility is important, a consistent user interface is also provided.

So, I put together a class extending JLabel based on McDowell code. It is self-sufficient, correctly processes errors and looks more like a link:

 public class SwingLink extends JLabel { private static final long serialVersionUID = 8273875024682878518L; private String text; private URI uri; public SwingLink(String text, URI uri){ super(); setup(text,uri); } public SwingLink(String text, String uri){ super(); setup(text,URI.create(uri)); } public void setup(String t, URI u){ text = t; uri = u; setText(text); setToolTipText(uri.toString()); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { open(uri); } public void mouseEntered(MouseEvent e) { setText(text,false); } public void mouseExited(MouseEvent e) { setText(text,true); } }); } @Override public void setText(String text){ setText(text,true); } public void setText(String text, boolean ul){ String link = ul ? "<u>"+text+"</u>" : text; super.setText("<html><span style=\"color: #000099;\">"+ link+"</span></html>"); this.text = text; } public String getRawText(){ return text; } private static void open(URI uri) { if (Desktop.isDesktopSupported()) { Desktop desktop = Desktop.getDesktop(); try { desktop.browse(uri); } catch (IOException e) { JOptionPane.showMessageDialog(null, "Failed to launch the link, your computer is likely misconfigured.", "Cannot Launch Link",JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(null, "Java is not able to launch links on your computer.", "Cannot Launch Link", JOptionPane.WARNING_MESSAGE); } } } 

You can also, for example, change the color of the link to purple by clicking if it is useful. All this by itself, you just call:

 SwingLink link = new SwingLink("Java", "http://java.sun.com"); mainPanel.add(link); 
+12
Dec 23 '10 at 11:53
source share

You can try using JEditorPane instead of JLabel. This is understood by basic HTML and dispatches a HyperlinkEvent event to the HyperlinkListener that you are registering with JEditPane.

+10
Oct 21 2018-11-21T00:
source share

If <a href = "link"> does not work, then:

  • Create a JLabel and add a MouseListener (decorate the shortcut so that it looks like a hyperlink).
  • Introduce the mouseClicked () event
  • In the implementation of the mouseClicked () event, execute your action

Take a look at the java.awt.Desktop API to open the link using the default browser (this API is only available with Java6).

+4
Feb 09 '09 at 11:05
source share

I know I'm a little late to the party, but I made a little method that others might find cool / useful.

 public static JLabel linkify(final String text, String URL, String toolTip) { URI temp = null; try { temp = new URI(URL); } catch (Exception e) { e.printStackTrace(); } final URI uri = temp; final JLabel link = new JLabel(); link.setText("<HTML><FONT color=\"#000099\">"+text+"</FONT></HTML>"); if(!toolTip.equals("")) link.setToolTipText(toolTip); link.setCursor(new Cursor(Cursor.HAND_CURSOR)); link.addMouseListener(new MouseListener() { public void mouseExited(MouseEvent arg0) { link.setText("<HTML><FONT color=\"#000099\">"+text+"</FONT></HTML>"); } public void mouseEntered(MouseEvent arg0) { link.setText("<HTML><FONT color=\"#000099\"><U>"+text+"</U></FONT></HTML>"); } public void mouseClicked(MouseEvent arg0) { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(uri); } catch (Exception e) { e.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("Could not open link."); JDialog dialog = pane.createDialog(new JFrame(), ""); dialog.setVisible(true); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }); return link; } 

This will give you a JLabel that acts as the correct link.

In action:

 public static void main(String[] args) { JFrame frame = new JFrame("Linkify Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 100); frame.setLocationRelativeTo(null); Container container = frame.getContentPane(); container.setLayout(new GridBagLayout()); container.add(new JLabel("Click ")); container.add(linkify("this", "http://facebook.com", "Facebook")); container.add(new JLabel(" link to open Facebook.")); frame.setVisible(true); } 

If you don't need a hint, just send zero.

Hope someone finds this helpful! (If you do, be sure to let me know, I will be happy to hear.)

+4
Dec 15 '12 at 19:09
source share

Use JEditorPane with HyperlinkListener .

+4
Dec 10
source share

Just put window.open(website url) , it works every time.

+2
Feb 19 '15 at 15:16
source share

The following code requires JHyperLink to add to your build path.

 JHyperlink stackOverflow = new JHyperlink("Click HERE!", "https://www.stackoverflow.com/"); JComponent[] messageComponents = new JComponent[] { stackOverflow }; JOptionPane.showMessageDialog(null, messageComponents, "StackOverflow", JOptionPane.PLAIN_MESSAGE); 

Note that you can populate the JComponent array with additional Swing components.

Result:

+1
Apr 30 '15 at 9:09
source share

You can use this under

 actionListener -> Runtime.getRuntime().exec("cmd.exe /c start chrome www.google.com")` 

or if you want to use Internet Explorer or Firefox, replace chrome with iexplore or firefox

+1
Dec 25 '15 at 22:02
source share



All Articles