I've followed guidelines here: http://xmlgraphics.apache.org/batik/faq.html and read through pages of their mailing list. I've tried everything I can think of. Here is a very simple example of what I am trying to accomplish. Layout manager is: http://java.sun.com/products/jfc/tsc/articles/tablelayout/ Or you can use gridbag or whatever you like. XmlHelper is in-house lib, you can substitue with your own. Not included to cut down on code size.
Basically after you hit the "up" button (short for update), it modifies the DOM and adds a new element, yet i dont see the SVG update.
..
public class SvgRefresh extends JFrame
{
private static final NAMESPACE = "http://www.w3.org/2000/svg";
private JSVGCanvas canvas;
private Document document;
private JButton button;
public static void main(String[] args)
{
SvgRefresh svgRefresh = new SvgRefresh();
svgRefresh.pack();
svgRefresh.setVisible(true);
}
public SvgRefresh()
{
canvas = new JSVGCanvas();
initialize();
}
private void initialize()
{
double[][] size = {{ TableLayout.FILL, 0.1, TableLayout.FILL}, { TableLayout.FILL, 0.1 }};
getContentPane().setLayout(new TableLayout(size));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
document = XmlHelper.readDocumentFromFile("F:/SVG/svg01.svg");
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.setDocument(document);
document = canvas.getSVGDocument();
getContentPane().add(canvas, "0, 0, 2, 0");
getContentPane().add(getButton(), "1, 1");
}
private JButton getButton()
{
if(button == null)
{
button = new JButton("Up");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
UpdateManager updateManager = canvas.getUpdateManager();
updateManager.getUpdateRunnableQueue().invokeLater(new Runnable()
{
@Override
public void run()
{
Element root = document.getDocumentElement();
Element text = document.createElementNS(NAMESPACE, "text");
text.setAttributeNS(NAMESPACE, "x", "100");
text.setAttributeNS(NAMESPACE, "y", "100");
text.setTextContent("It worked!!!");
root.appendChild(text);
System.out.println("ran");
}
});
}
});
}
return button;
}
}
Original SVG (made in AI, and i edited all content out except for the text element):
<?xml version="1.0" encoding="UTF-8"?><svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="application/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" enable-background="new 0 0 400 200" version="1.1" xml:space="preserve" width="400px" preserveAspectRatio="xMidYMid meet" viewBox="0 0 400 200" height="200px" x="0px" y="0px">
<text x="40" y="40">Default text</text>
</svg>
Code above doesn't include writing to file - I've excluded for space. SVG after pressing UP button and written to file (to check namespace). When written to file after pressing "UP" button this is produced - which looks perfect to me.
<?xml version="1.0" encoding="UTF-8"?><svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="application/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" enable-background="new 0 0 400 200" version="1.1" xml:space="preserve" width="400px" preserveAspectRatio="xMidYMid meet" viewBox="0 0 400 200" height="200px" x="0px" y="0px">
<text x="40" y="40">Default text</text>
<text x="100" y="100">It worked!!!</text>
</svg>
Thanks for the help. Oh and to encourage research, and people taking time with their answers, I will probably wait around 24 hours before awarding an answer. So take your time and run the code if you want.
Just change
text.setAttributeNS(NAMESPACE, "x", "100");
text.setAttributeNS(NAMESPACE, "y", "100");
to
text.setAttributeNS(null, "x", "100");
text.setAttributeNS(null, "y", "100");
and it will update fine