Java hyperlink - a JLabel hyperlink example

This Java class still needs some work, but it's my first attempt at creating a Java Swing component that simulates a hyperlink. Basically, if you use this label (a JHyperlinkLabel to be precise) instead of a JLabel you should see hyperlink behavior on your Swing labels (JLabel).

Again, this is a work in progress, so don't get too excited, but I think it has promise. The nice thing is that users can click the label to trigger an event, so it works a lot like hyperlinks on the web.

Java hyperlink (JLabel hyperlink) source code

Without any further ado, here is the code:

package com.devdaily.gui.utils;

import java.awt.Cursor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;

public class JHyperlinkLabel extends JLabel {
  
  private String url;

  final Color COLOR_NORMAL    = Color.BLUE;
  final Color COLOR_HOVER     = Color.RED;
  final Color COLOR_ACTIVE    = COLOR_NORMAL;
  final Color COLOR_BG_NORMAL = Color.LIGHT_GRAY;
  final Color COLOR_BG_ACTIVE = Color.LIGHT_GRAY;
  Color mouseOutDefault;

  public JHyperlinkLabel(String text, String url) {
    super(text);
    this.url = url;
  }

  public JHyperlinkLabel() {
    addMouseListener();
    setForeground(COLOR_NORMAL);
    setBackground(COLOR_BG_NORMAL);
    mouseOutDefault = COLOR_NORMAL;
    this.setSize((int)this.getPreferredSize().getWidth(),(int)this.getPreferredSize().getHeight());
    this.setOpaque(true);
  }
  
  public void setUrl(String url) {
    this.url = url;
    setText("" + getText() + "");
  }
  
  public void setText(String text) {
    super.setText(text);
  }
  
  public void paint(Graphics g) {
    super.paint(g);
    g.drawLine(2,
               getHeight()-1,
               (int)getPreferredSize().getWidth()-2,
               getHeight()-1);
  }

  public void addMouseListener() {

    addMouseListener( new MouseListener() {

      public void mouseClicked(MouseEvent me) {
        setForeground(COLOR_ACTIVE);
        mouseOutDefault = COLOR_ACTIVE;
        // do something here
      }
      
      public void mouseReleased(MouseEvent me) {
        
      }
      
      public void mousePressed(MouseEvent me) {
        mouseOutDefault = COLOR_ACTIVE;
      }
      
      public void mouseEntered(MouseEvent me) {
        setForeground(COLOR_HOVER);
        setBackground(COLOR_BG_ACTIVE);
        Cursor cursor = getCursor();
        setCursor(cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
      }
      
      public void mouseExited(MouseEvent me) {
        setForeground(mouseOutDefault);
        setBackground(COLOR_BG_NORMAL);
        Cursor cursor = getCursor();
        setCursor(cursor.getDefaultCursor());
      }
    });
  }

}

I need to note that I got some of this code from another site on the Internet ... I'm sorry, but I can't remember where. I've changed it a fair amount since then, but I'd still like to give the original author some credit.