This code will allow you to put a hyperlink in your application using the label class.


================= URLLabel Class =================
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class URLLabel extends Label {
private java.applet.Applet _applet;
private URL _url;
private String _target = "";
private Color _unvisitedURL = Color.blue;
private Color _visitedURL = Color.green;

public URLLabel(java.applet.Applet applet , String url, String text){
this(applet, url, text, "_self");
}

public URLLabel(java.applet.Applet applet , String url, String text, String target){
super(text);
setForeground(_unvisitedURL);
try {
_applet = applet;
_url = new URL(url);
_target = target;
addMouseListener( new Clicked() );
}
catch (Exception e) {
e.printStackTrace();
}
}

public void paint(Graphics g) {
Rectangle r;
super.paint(g);
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());
}

public void setUnvisitedURLColor(Color c) {
_unvisitedURL = c;
}

public void setVisitedURLColor(Color c) {
_visitedURL = c;
}

class Clicked extends MouseAdapter{
public void mouseClicked(MouseEvent me){
setForeground(_visitedURL);
_applet.getAppletContext().showDocument(_url, _target);
}
}
}

================ URL Test class ==============
import java.applet.*;
import java.awt.*;

public class TestURLLabel extends Applet {
public void init() {
URLLabel ull1 =
new URLLabel
(this, "
http://www.yahoo.com", "Click here to go to Yahoo");
add(ull1);
URLLabel ull2 =
new URLLabel
(this, "http://www.google.com", "Click here to go to Google");
add(ull2);
URLLabel ull3 =
new URLLabel
(this, "http://learn.elementk.com", "Click here to go to ElementK");
add(ull3);
URLLabel ull4 =
new URLLabel
(this, "http://java.sun.com/", "Click here to go to the Java site");
add(ull4);
validate();
}
}

============== HTML Page ====================
< HTML>< HEAD>< /HEAD>< BODY>
< APPLET CODE="TestURLLabel.class"
NAME="URL Test"
HEIGHT=200 WIDTH=200>
< /APPLET>< /BODY>< /HTML>