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 javax.swing.JLabel;

 * @author ludovicianul
public class URLLabel extends JLabel {

    private String url;

    public URLLabel() {

    public URLLabel(String label, String url) {

        this.url = url;
                new Cursor(Cursor.HAND_CURSOR));
                new URLOpenAdapter());

    public void setURL(String url) {
        this.url = url;

    //this is used to underline the text
    protected void paintComponent(Graphics g) {


        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 {

        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 Desktop class 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 linked 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;
    protected void paintComponent(Graphics g) {

        g.setColor(clicked? Color.BLUE.darker());
        public void mouseClicked(MouseEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(new URI(url));

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 =;
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 bellow.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s