Hyperlink Events

JEditorPane s fire a type of event called a HyperlinkEvent. Typically, this event is fired when the user clicks on a hyperlink in the currently displayed document; the program normally responds by loading a new page. To support this event type, a related event class and listener interface are available in the javax.swing.event package.

The HyperlinkListener Interface

The HyperlinkListener interface (found in javax.swing.event) defines a single method, used to respond to hyperlink activations:

public abstract void hyperlinkUpdate(HyperlinkEvent e)

Called to indicate that a hyperlink request has been made. Typical implementations of this method obtain the new URL from the event and call setPage( ) on the associated JEditorPane. See the JEditorPane example earlier in the chapter to learn how this method can be used.

The HyperlinkEvent Class

The HyperlinkEvent class (found in javax.swing.event) describes a hyperlink request.

Properties

HyperlinkEvent defines the properties shown in Table 23-9. The URL property contains a java.net.URL object that can be used to retrieve URL content represented by the event. The description property allows a description of the link (typically, the text of the hyperlink) to be supplied. This can be useful when the URL property is null, such as when the hyperlink can’t be parsed well enough to create a URL object. The eventType property defines the type of event that has occurred.

Table 23-9. HyperlinkEvent properties

Property

Data type ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.