HyperlinkEvent.getSourceElement annoying

J

Jim Cobban

I am in the process of adding support for the target attribute of the
"anchor" tag into the help display portion of my application. So I had to
find the value of the target attribute. Looking at the documentation for
JDK 1.4.2 it appears that should be available through the new
HyperlinkEvent.getSourceElement method.

I find it annoying that this method returns javax.swing.text.Element rather
than org.w3c.dom.Element particularly because the former class has an
obscure and over-generalized interface compared to the latter. The
documentation for the former states "It is intended to capture the spirit of
an SGML element." Well if it is intended to capture the spirit of the SGML
element why the h*** didn't they just use the SGML element? Especially
since org.w3c.dom.Element is also part of J2SE 1.4.2.

--
Jim Cobban (e-mail address removed)
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438
 
J

Jim Cobban

Jim Cobban said:
I am in the process of adding support for the target attribute of the
"anchor" tag into the help display portion of my application. So I had to
find the value of the target attribute. Looking at the documentation for
JDK 1.4.2 it appears that should be available through the new
HyperlinkEvent.getSourceElement method.

Just in case anyone else wants to do something similar I thought I would
just let you know the loops that you have to run through.

HyperlinkEvent.getSourceElement does not return the HTML element whose
selection triggered the event, although that is what the documentation
implies. Rather it returns a constructed element that contains two
attributes, the one with key javax.swing.text.html.HTML.Tag.A has a value
which contains a javax.swing.text.AttributeSet containing the attributes of
the original HTML element.

So, ignoring error recovery, the code to get to the target attribute is:

public void hyperlinkUpdate(HyperlinkEvent ev)
{
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
{
Element anchor;
anchor = ev.getSourceElement();
AttributeSet attrs;
attrs = anchor.getAttributes();
attrs = (AttributeSet)attrs.getAttribute(

javax.swing.text.html.HTML.Tag.A);
String target;
target = (String)attrs.getAttribute(

javax.swing.text.html.HTML.Attribute.TARGET);
if (target != null)
{
System.err.println("Display the page in new window: target=\"" +
target + "\"");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top