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

Recommendations


About these ads

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s