How to serialize JavaFX Nodes preferably in XML?

扶醉桌前 提交于 2020-01-15 09:47:11

问题


I have some kind of drawpane, where the user can draw different kinds of Shapes, like Pathes, Rectangles, Texts, etc. Now I´d like to persist the DrawState, so that I can recreate it at any time.

I just tried it with xstream, but like usual I get a whole lot of dependencies within these Shapes when persisting it in XML. After some research I found this Thread here:

Serialize JavaFX components

I liked the idea of jewelsea using the SceneBuilder API to serialize my shapes, since they are all supported and the FXML contains all the things I probably will need without any disturbing references.

Do you have any experiences with the Scenebuilder API? In the thread its pointed out, that it might be difficult to get only the things needed. Or can you maybe recommend any other way to serialize Shapes in XML?


回答1:


Using annotations to describe what data to save, JAXB makes it very simple to save/load objects to/from XML.

You'll probably want to create template objects for each JavaFX object so you can control what data you are storing. Here's a tutorial on using JAXB.




回答2:


This might come a bit late, but if you use XStream, note that we shared a collection of Converters for JavaFX types: see https://github.com/dooApp/XStreamFX



来源:https://stackoverflow.com/questions/24780785/how-to-serialize-javafx-nodes-preferably-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!