简体   繁体   English

如何序列化具有ObservableList的对象?

[英]How to serialize an object that has an ObservableList on it?

I actually want to serialize a List of objects but I think that makes no diference. 我实际上想序列化一个对象列表,但我认为没有区别。 I am putting my class that has just two properies. 我正在上课,只有两个属性。 A string and a ObservableList. 一个字符串和一个ObservableList。 I will include the serialization methods in it. 我将在其中包含序列化方法。 I know ObservableList does not implements serializable, but I am populating a TableView with them, that is why I am using it but if there is another way, I am open to it. 我知道ObservableList没有实现可序列化,但我用它们填充了TableView,这就是我使用它的原因,但是如果有另一种方式,我对它开放。

public class Tratamiento implements Serializable{

private String name;
private ObservableList<Material> materials;

public Tratamiento(String name, ObservableList<Material> materials) {
    this.name= name;
    this.materials= materials;
}

public Treatment(){        
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name= name;
}

public ObservableList getMaterials() {
    return materials;
}

public void setMaterials(ObservableList materials) {
    this.materials = materials;
}


public int addTreatment(List tratamiento) {
    int res = 0; //Failure

    try {
        FileOutputStream fileOut = new FileOutputStream("Treatment.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(treatment);
        out.close();
        fileOut.close();
        System.out.printf("Object has been serialized");
        res = 1;
    } catch (IOException i) {
        i.printStackTrace();

    }

    return res;
}



public List consultListTreatments() {
    int res = 0;
    //Deserialize file
    List<Treatment> list = new ArrayList<Treatment>();

    try {
        FileInputStream fileIn = new FileInputStream("Treatment.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        list = (List) in.readObject();     
        in.close();
        fileIn.close();
        res = 1;
    } catch (IOException i) {
        i.printStackTrace();
    } catch (ClassNotFoundException c) {
        System.out.println("Class not found");
        c.printStackTrace();
    }    
    return list;
}    

}

Here is the implementation in my FXML controller 这是我的FXML控制器中的实现

@FXML
private void createTreatment(ActionEvent event){
   Treatment tr = new Treatment();        
    List<Treatment> list = new ArrayList<Treatment>();
    list = tr.consultListTreatments();

    ObservableList<Material> rows = table.getItems();//FXCollections.observableArrayList();
    //get values of the rows


    Treatament newTreatment = new Treatment(TextFieldTreatmentName.getText(),rows);

    list.add(newTreatment);
    newTreatment.addTreatment(list);  
    TextFieldTreatmentName.clear();
}

I saw this question but it does not fit my need nor did I could modify it to what I want. 我看到了这个问题,但它不符合我的需要,也没有把它修改为我想要的东西。 How to serialize ObservableList 如何序列化ObservableList

In this case, you will have to implement the Externalizable interface. 在这种情况下,您必须实现Externalizable接口。 This will require you to write your own methods for serializing and deserializing your objects. 这将要求您编写自己的方法来序列化和反序列化对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM