URL Label : JLabel « Swing « Java Tutorial






//The contents of this file are subject to the Mozilla Public License Version 1.1
//(the "License"); you may not use this file except in compliance with the 
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003. 
//
//All Rights Reserved.

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.JLabel;

public class URLLabel extends JLabel {

  boolean entered = false;

  boolean mousehover;

  public URLLabel(URL url) {
    this(url, url.toString());
  }

  public URLLabel(URL url, String str) {
    super(str);

    setForeground(Color.blue);
    mousehover = false;

  }

  public void mouseEntered(MouseEvent e) {
    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    entered = true;

    if (mousehover) {
      repaint();
    }
  }

  public void mouseExited(MouseEvent e) {
    setCursor(Cursor.getDefaultCursor());
    entered = false;

    if (mousehover) {
      repaint();
    }
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void paint(Graphics g) {
    super.paint(g);

    if (entered || !mousehover) {
      Rectangle r = g.getClipBounds();

      g.drawLine(0, r.height - this.getFontMetrics(this.getFont()).getDescent(), this
          .getFontMetrics(this.getFont()).stringWidth(this.getText()), r.height
          - this.getFontMetrics(this.getFont()).getDescent());
    }
  }
}








14.3.JLabel
14.3.1.JLabel
14.3.2.Create JLabel component
14.3.3.Create a JLabel with an image icon
14.3.4.JLabel is for displaying text, images or both. It does not react to input events.
14.3.5.Horizontal Alignment: CENTER
14.3.6.Vertical Alignment: CENTER
14.3.7.Horizontal Alignment: LEFT
14.3.8.Vertical Alignment: TOP
14.3.9.Horizontal Alignment: RIGHT
14.3.10.Vertical Alignment: BOTTOM
14.3.11.The text is horizontally and vertically centered
14.3.12.The text is right-justified and vertically centered
14.3.13.The text is left-justified and top-aligned
14.3.14.The text is right-justified and top-aligned
14.3.15.The text is left-justified and bottom-aligned
14.3.16.The text is right-justified and bottom-aligned
14.3.17.Add Icon to JLabelAdd Icon to JLabel
14.3.18.Mix Icon and text in JLabelMix Icon and text in JLabel
14.3.19.Set Font and foreground color for a JLabelSet Font and foreground color for a JLabel
14.3.20.Load image from disk file and add it to a JLabelLoad image from disk file and add it to a JLabel
14.3.21.Using JLabel Mnemonics: Interconnect a specific JLabel and JTextField.Using JLabel Mnemonics: Interconnect a specific JLabel and JTextField.
14.3.22.Using a JLabel and pass in HTML for the textUsing a JLabel and pass in HTML for the text
14.3.23.Multiline label (HTML)
14.3.24.JLabel with more than one row
14.3.25.A simple Bean which extends JLabel
14.3.26.Add JLabel to JScrollPane
14.3.27.Disable a label
14.3.28.Unicode with Label
14.3.29.Customizing a JLabel Look and Feel
14.3.30.Associate JLabel component with a JTextField
14.3.31.Setting the Focus of a JTextField Component Using a JLabel Component
14.3.32.JLabel with lined border
14.3.33.A Label that uses inline HTML to format its text
14.3.34.A label with no indication it has been clicked
14.3.35.Adding Drag-and-Drop Support to a JLabel Component
14.3.36.Have a Label with underlined text
14.3.37.Multi Line Label
14.3.38.Vertical Label UI
14.3.39.Time panel shows the current time.
14.3.40.URL Label
14.3.41.Hyperlink Label
14.3.42.Gradient Label