Hyperlink Label : Label « Swing Components « Java

Hyperlink Label


import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;

public class JHyperlinkLabel extends JLabel {
  private Color underlineColor = null;

  public JHyperlinkLabel(String label) {

    setCursor(new Cursor(Cursor.HAND_CURSOR));
    addMouseListener(new HyperlinkLabelMouseAdapter());

  protected void paintComponent(Graphics g) {

    g.setColor(underlineColor == null ? getForeground() : underlineColor);

    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);

  public class HyperlinkLabelMouseAdapter extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {

  public Color getUnderlineColor() {
    return underlineColor;

  public void setUnderlineColor(Color underlineColor) {
    this.underlineColor = underlineColor;


Related examples in the same category

1.MultiLine Label
2.Multi Line Label extends JComponent
3.Multi Line Label extends JPanel
4.Link Label
5.Vertical Label UI
6.Label with large font and ANTIALIAS paint
7.Label 3D
8.URL Label
9.Bevel TextBevel Text
10.A JLabel that can be underlined, implements Scrollable
11.Gradient Label
12.Hyperlink Label 2
13.Computes a reasonable set of labels for a data interval and number of labels.
14.Multi-label Label
15.Shadow Label
16.Button Label