Hyperlink Label 2 : Label « Swing Components « Java






Hyperlink Label 2

    
/**
 * @(#)HyperlinkLabel.java
 */

//package aurora.util;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;


/**
 * Implementation of 
 * @author Alex Kurzhanskiy
 * @version $Id: HyperlinkLabel.java 38 2010-02-08 22:59:00Z akurzhan $
 */
public class HyperlinkLabel extends JLabel implements MouseListener {
  private static final long serialVersionUID = 5167616594614061634L;
  
  private URL url = null;
  
  
  public HyperlinkLabel(String label){
    super(label);
    addMouseListener(this);
  }
  public HyperlinkLabel(String label, URL url){
    this(label);
    this.url = url;
    setText("<html><a href=\"\">" + label + "</a></html>");
    setToolTipText("Go to: " + url.getRef());
  }
  public HyperlinkLabel(String label, String tip, URL url){
    this(label, url);
    setToolTipText(tip);
  }
  
  
  public void setURL(URL url){ this.url = url; }
  
  public URL getURL(){ return url; }

  public void mouseClicked(MouseEvent e) {
    HyperlinkLabel self = (HyperlinkLabel) e.getSource();
    if(self.url == null)
      return;
    if (Desktop.isDesktopSupported()) {
      Desktop desktop = Desktop.getDesktop();
      if (desktop.isSupported(Desktop.Action.BROWSE))
        try{
          desktop.browse(url.toURI());
          return;
        }
      catch(Exception exp){ }
    }
    JOptionPane.showMessageDialog(this, "Cannot launch browser...\n Please, visit\n" + url.getRef() , "", JOptionPane.INFORMATION_MESSAGE);
    return;
  }

  public void mouseEntered(MouseEvent e) {
    e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    return;
  }

  public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    return;
  }

  public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    return;
  }

  public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    return;
  }
  
}

   
    
    
    
  








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.Hyperlink Label
10.Bevel TextBevel Text
11.A JLabel that can be underlined, implements Scrollable
12.Gradient Label
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