This is how I add event listeners:
* Get the element out of the document.
* Access the eventtarget out of the element and add the listener to it.
element = (SVGElement) document.getElementById("someElement");
if (element!= null) {
EventTarget target = (EventTarget) element;
target.addEventListener("mouseover",
new OnMouseOverListener(), false);
target.addEventListener("mouseout",
new OnMouseOutListener(), false);
} else {
....
I tend to add a gvttreebuilderlistener to the canvas to know when the svg is
ready to be access.
public void gvtBuildStarted(GVTTreeBuilderEvent event) {}
}
public void gvtBuildCompleted(GVTTreeBuilderEvent event) {}
And when making an update to a SVG element you should do it inside a Runnable
interface.
Hope this helps.
-Jose
From: Bachir Assi [mailto:[email protected]]
Sent: Monday, October 17, 2011 8:56 AM
To: [email protected]
Subject: EXTERNAL: Batik - EventHandler
Hello,
I'm trying to open an SVG file in JAVA using batik-1.7 library, and add some
mouse events to it.
But this is not possible unless i add a "script" tag to the SVG file.
(If i don't write "<script> </script>" in the SVG file, i cannot access the
"handleEvent" method)
(Windows 7, jdk1.6.0_20_x32)
Here is a part of my JAVA file :
-------------------------------------------------------------------------------------------------------
svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
label.setText("Document Loading...");
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
label.setText("Document Loaded.");
addListeners(svgCanvas.getSVGDocument().getRootElement());
}
});
private void addListeners(SVGSVGElement element) {
element.addEventListener("mousedown"
, new EventListener() {
public void
handleEvent(Event evt) {
/*Add
handling code*/
}
}
, false);
}
-------------------------------------------------------------------------------------------------------
The SVG file that i'm loading is "circles.svg" :
-------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<script>
</script>
<svg xmlns="http://www.w3.org/2000/svg">
<g style="fill-opacity:0.7;">
<circle cx="6.5cm" cy="2cm" r="100" style="fill:red; stroke:black;
stroke-width:0.1cm" transform="translate(0,50)" />
<circle cx="6.5cm" cy="2cm" r="100" style="fill:blue; stroke:black;
stroke-width:0.1cm" transform="translate(70,150)" />
<circle cx="6.5cm" cy="2cm" r="100" style="fill:green; stroke:black;
stroke-width:0.1cm" transform="translate(-70,150)"/>
</g>
</svg>
-------------------------------------------------------------------------------------------------------
How can i be able to access the eventHandler without adding a script tag (The
RED part in SVG file) ?
Thank you for your help, and hope to hear from you soon
Regards,
Bashir