Search code examples
javasvgbatik

JSVGCanvas.getSVGDocument() is returning null?


I seem to have a problem working with batikSVG for manupilating SVG using Java. I can display the SVG just fine on the JSVG Canvas but when I try to the canvas's SVGDocument using getSVGDocument it seems to return null. Why is that, and how can I get the actual document?

jSVGCanvas1.setURI(new File("circle.svg").toURI().toString());

    jSVGCanvas1.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
    SVGDocument doc =   jSVGCanvas1.getSVGDocument();
    if(doc==null)System.out.println("null");

The last line tests where doc is null and it always prints null. Please help!


Solution

  • You'll need to wait for the document to load and that happens asynchronously. Something like this...

       jSVGCanvas1.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
              SVGDocument doc = jSVGCanvas1.getSVGDocument();
              if(doc==null)System.out.println("null");
            }
        });