Creating a URL JLabel in Swing
So, you need a simple clickable URL JLabel in your Swing application? You can, of course, use third party libraries to do this, but why add another dependency for a single component when can easily write one in 5 minutes. So what do we want for this JLabel: 
- to click on it and go to an URL
- to have a hand cursor on mouse over
- the text to be underlined just like a real link
We'll write a class that extends JLabel and a mouse adapter that will take care of opening the link. As the mouse adapter will be used only for this label, we'll add it as a private member class. Here is the code: 
package insidecoding.swing
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import javax.swing.JLabel;
/**
 *
 * @author ludovicianul
 */
public class URLLabel extends JLabel {
    private String url;
    public URLLabel() {
        this("","");
    }
    public URLLabel(String label, String url) {
        super(label);
        this.url = url;
        setForeground(Color.BLUE.darker());
        setCursor(
                new Cursor(Cursor.HAND_CURSOR));
        addMouseListener(
                new URLOpenAdapter());
    }
    public void setURL(String url) {
        this.url = url;
    }
    //this is used to underline the text
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.blue);
        Insets insets = getInsets();
        int left = insets.left;
        if (getIcon() != null) {
            left += getIcon().getIconWidth() + getIconTextGap();
        }
        g.drawLine(left, getHeight() - 1 - insets.bottom, 
			(int) getPreferredSize().getWidth()
                - insets.right, getHeight() - 1 - insets.bottom);
    }
    private class URLOpenAdapter extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(new URI(url));
                } catch (Throwable t) {
                    //
                }
            }
        }
    }
}
I think the code is pretty self-explanatory:
- In the constructor, we set the label color to a darker blue, we set the mouse over cursor to Hand Cursor and we register the mouse click listener
- We use the paint()method to underline the label's text.
- The mouse adapter uses the Desktopclass to open the browser.
- This code does not treat exceptions when opening the URL; this depends on the program needs.
We can add an additional feature like changing color once the link was clicked in a working session. To do this, we add a private boolean variable that will hold the click state, change the paint method to consider this flag and mark it as true when the label is clicked: 
    private boolean clicked;
...
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(clicked? Color.black: Color.BLUE.darker());
....
        @Override
        public void mouseClicked(MouseEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(new URI(url));
                    clicked=true;
.....
Furthermore, you make the label color and the clicked color variable. We declare 2 new member variables: normalColor and clickedColor and we declare another constructor where we can send them as parameters: 
private Color clickedColor = Color.black;
private Color normalColor  = Color.BLUE.darker();
public URLLabel(String label, String url, Color clickedColor, Color normalColor) {
        this(label, url);
        this.clickedColor = clickedColor;
        this.normalColor = normalColor;
}
If you have questions, please leave a comment below.
Post Comment
- iGtJ65 Great article.Really thank you! Awesome. 
- EDJyO6 wow, awesome blog post. Cool. 
- Nsxy1d Looking forward to reading more. Great article post.Thanks Again. Will read on... 
- 2PwQM1 Muchos Gracias for your post.Really looking forward to read more. Great. 
 matzcrorkz[37.233.27.*]2014/8/5 14:35:38#4
matzcrorkz[37.233.27.*]2014/8/5 14:35:38#4