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

查看:150
本文介绍了序列化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组件。我试图序列化Buttons,Scenes,Stages,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 ?

PS:我试图用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天全站免登陆