私はここのガイドラインに従いました:http://xmlgraphics.apache.org/batik/faq.htmlそして彼らのメーリングリストのページを読み通しました。私は考えられるすべてを試しました。これが私が達成しようとしていることの非常に簡単な例です。レイアウトマネージャーは次のとおりです。http://java.sun.com/products/jfc/tsc/articles/tablelayout/または、gridbagなどを使用できます。XmlHelperは社内ライブラリであり、独自のライブラリに置き換えることができます。コードサイズを削減するために含まれていません。
基本的に、「上へ」ボタン(更新の略)を押すと、DOMが変更され、新しい要素が追加されますが、SVGの更新は表示されません。
..
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;
}
}
オリジナルのSVG(AIで作成され、テキスト要素を除くすべてのコンテンツを編集しました):
<?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>
上記のコードにはファイルへの書き込みは含まれていません-スペースを除外しました。UPボタンを押してファイルに書き込んだ後のSVG(名前空間を確認するため)。「UP」ボタンを押した後にファイルに書き込むと、これが生成されます-これは私には完璧に見えます。
<?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>
助けてくれてありがとう。ああ、研究を奨励するために、そして人々が彼らの答えに時間を割いているので、私はおそらく答えを与える前に約24時間待つでしょう。したがって、時間をかけて、必要に応じてコードを実行してください。