序列化 JavaFX 组件 [英] Serialize JavaFX components

查看:15
本文介绍了序列化 JavaFX 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

重要的是存储关于在某个位置放置的所有对象的信息.

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

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

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).

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

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

P.S.:我试图用 FXML 找出一些方法,但没有成功.

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

非常感谢您的回答:)

Thank you very much for your answers :)

推荐答案

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

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;
}
}

这篇关于序列化 JavaFX 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆