Java Reference
In-Depth Information
public HyperlinkEvent.EventType getEventType();
public Element getSourceElement();
public URL getURL();
}
The hyperlink event types will be one of three constants within the
HyperlinkEvent.EventType
class:
•
ACTIVATED
: Usually involving a mouse click over the appropriate content
•
ENTERED
: Moving the mouse over the hyperlink content
•
EXITED
: Moving the mouse out of the hyperlink content
Therefore, if you want to create a
HyperlinkListener
that displays the URL in a status bar
while over a hyperlink and follows the hyperlink when activated, you can create your own
miniature HTML help viewer. The
HyperlinkListener
implementation in Listing 15-15 will do
the trick for you. There are
println
statements present in the listener to display the URL when
the mouse is over the URL and when the URL is activated.
Listing 15-15.
HyperlinkListener Example
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
public class ActivatedHyperlinkListener implements HyperlinkListener {
Frame frame;
JEditorPane editorPane;
public ActivatedHyperlinkListener(Frame frame, JEditorPane editorPane) {
this.frame = frame;
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url );
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Runnable runner = new Runnable() {
public void run() {