序列化JavaFX组件
我正在尝试在Java
FX下开发一个拖放应用程序。用户将在某些位置放置JFX组件,例如按钮,菜单,标签。完成后,他将保存此布局,稍后再重新打开该布局,然后将再次使用它。
重要的是存储有关放置在某个位置的所有对象的信息。
我决定为此目的使用序列化。但是我无法序列化JavaFX组件。我试图序列化Buttons,Scenes,Stages,JFXPane,但似乎没有任何效果(我获得了NotSerializableException)。
有什么建议如何保存所有组件然后检索它们?
PS:我试图用FXML找出某种方法,但没有成功。
非常感谢您的回答:)
回答:
如果将用户组件保存在服务器端的主要目的是-可以向用户显示相同的界面-为什么不保存您需要的有关用户组件的所有描述性信息,以及何时需要-
只需重建用户界面再次使用存储的描述性信息?这是原始示例:
/* 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组件 的全部内容, 来源链接: utcz.com/qa/403176.html