I l@ve RuBoard |
![]() ![]() |
23.4 Hyperlink EventsJEditorPane 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. 23.4.1 The HyperlinkListener InterfaceThe HyperlinkListener interface (found in javax.swing.event) defines a single method, used to respond to hyperlink activations:
23.4.2 The HyperlinkEvent ClassThe HyperlinkEvent class (found in javax.swing.event) describes a hyperlink request. 23.4.2.1 PropertiesHyperlinkEvent 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.
23.4.2.2 Constructors
23.4.2.3 Inner classes
23.4.3 The HTMLFrameHyperlinkEvent ClassAnother event that can arrive at your doorstep in the hyperlinkUpdate( ) method is the HTMLFrameHyperlinkEvent class from the javax.swing.text.html package. (We know, we know. Why isn't it part of the event package? Well, it is quite specific to HTML content whereas the concept of a hyperlink is supposed to be more generic.) This event is similar in content and purpose to the regular HyperlinkEvent and indeed extends that class. It carries information relating to HTML frames—specifically, the target frame for the hyperlink. target is the one new property this class adds, as shown in Table 23-10
If you look back at the code for the SimpleLinkListener class, you can see where we look for this type of event. ENTERED and EXITED hyperlink events are treated the same—we just update our status bar. However, if the user activates a link, we need to see where its destination lies. If the link is an instance of HTMLFrameHyperlinkEvent, we have to treat it differently. Fortunately, we don't have to do much work. The HTMLDocument class knows how to handle these events, so we just pass it to our document. We should note a few things about frame-related hyperlink events. They are not well-supported in SDK 1.2. They do exist there, but are probably best left out of the picture. Even in the 1.4 release, these events are not handled as gracefully as you might hope. For example, the _blank target does not open a new window in the current implementation of JEditorPane. To support such a target, you would need to monitor the target property of any incoming ACTIVATED HTMLFrameHyperlinkEvent object and launch your own new window if necessary. |
I l@ve RuBoard |
![]() ![]() |