java 序列化 JavaFX 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11033943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:32:05  来源:igfitidea点击:

Serialize JavaFX components

javaserializationuser-interfacejavafx

提问by Reshi

I'm trying to develop a little drag & drop application under Java FX. User will drop JFX components like Buttons, Menus, Labels on certain positions. When done, he will save this layout and later on he will reopen the layout and he will use it again.

我正在尝试在 Java FX 下开发一个小拖放应用程序。用户将在某些位置放置按钮、菜单、标签等 JFX 组件。完成后,他将保存此布局,稍后他将重新打开该布局并再次使用它。

Its important to store the information about all objects that are dropped on some position.

存储有关在某个位置放置的所有对象的信息很重要。

I decided to use serialization for this purpose. But I'm not able to serialize JavaFX components. I tried to serialize Buttons, Scenes, Stages, JFXPane but nothing seemed to work (I obtained NotSerializableException).

为此,我决定使用序列化。但我无法序列化 JavaFX 组件。我尝试序列化按钮、场景、阶段、JFXPane,但似乎没有任何效果(我获得了 NotSerializableException)。

Any suggestions how to save all the components and then retrieve them ?

任何建议如何保存所有组件然后检索它们?

P.S.: I was trying to find out some method with FXML but I did not succeed.

PS:我试图用 FXML 找出一些方法,但我没有成功。

Thank you very much for your answers :)

非常感谢您的回答:)

采纳答案by Agafonova Victoria

If the main goal of saving user components on the servers side - is to have a possibility to show the same interface to the user - why not to save all descriptive information you need about users components, and when it is needed - just rebuild user interface again, using stored descriptive information? Here is primitive example:

如果在服务器端保存用户组件的主要目标 - 是有可能向用户显示相同的界面 - 为什么不保存您需要的有关用户组件的所有描述信息,以及何时需要 - 只需重建用户界面再次,使用存储的描述信息?这是原始示例:

/* That is the class for storing information, which you need from your components*/
 public class DropedComponentsCoordinates implements Serializable{
private String componentID;
private String x_coord;
private String y_coord;
//and so on, whatever you need to get from yor serializable objects;
//getters and setters are assumed but not typed here.
 }

 /* I assume a variant with using FXML. If you don't - the main idea does not change*/
 public class YourController implements Initializable {

List<DropedComponentsCoordinates> dropedComponentsCoordinates;

@Override
public void initialize(URL url, ResourceBundle rb) {
    dropedComponentsCoordinates = new ArrayList();
}

//This function will be fired, every time 
//a user has dropped a component on the place he/she wants
public void OnDropFired(ActionEvent event) {
    try {
        //getting the info we need from components
        String componentID = getComponentID(event);
        String component_xCoord = getComponent_xCoord(event);
        String component_yCoord = getComponent_yCoord(event);

        //putting this info to the list
        DropedComponentsCoordinates dcc = new DropedComponentsCoordinates();
        dcc.setX_Coord(component_xCoord);
        dcc.setY_Coord(component_yCoord);
        dcc.setComponentID(componentID);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private String getComponentID(ActionEvent event){
    String componentID;
    /*getting cpmponentID*/
    return componentID;
}
private String getComponent_xCoord(ActionEvent event){
    String component_xCoord;
    /*getting component_xCoord*/
    return component_xCoord;
}
private String getComponent_yCoord(ActionEvent event){
    String component_yCoord;
    /*getting component_yCoord*/
    return component_yCoord;
}
}

回答by jewelsea

You are correct, JavaFX (as of 2.1) does not support serialization of components using the Java Serializableinterface - so you cannot use that mechanism.

您是对的,JavaFX(从 2.1 开始)不支持使用 Java Serializable接口的组件序列化- 因此您不能使用该机制。

JavaFX can deserialize from an FXML document using the FXMLLoader.load()method.

JavaFX 可以使用FXMLLoader.load()方法从 FXML 文档反序列化。

The trick though, is how to write your existing components and states out to FXML?

不过,诀窍是如何将现有组件和状态写入 FXML?

There is a forum discussionon serializing to FXML.

有一个关于序列化到 FXML的论坛讨论

Currently, there is nothing public from the platform which performs FXML serialization. Apparently, creating a generic scenegraph => FXML serializer is quite a complex task (and there is no public 3rd party API for this that I know of). It wouldn't be too difficult to iterate over the scenegraph and write out FXML for a limited set of components and attributes.

目前,执行 FXML 序列化的平台没有任何公开内容。显然,创建一个通用的场景图 => FXML 序列化器是一项相当复杂的任务(我所知道的没有公共的 3rd 方 API)。遍历场景图并为一组有限的组件和属性写出 FXML 不会太困难。