0

I've created these two functions and added them into an action listener within a button . The aim is to create a serialized file and write what is in the Jlist to the file. When the program is closed , the jlist should be populated with what is in the serialized file. For some reason it isn't working. Can anyone see what is wrong?

Here is the code:

        JButton btnAdd = new JButton("Add"); // Setting what is written on the button

        btnAdd.addActionListener(new ActionListener() { // implementing an action listener


            public void actionPerformed(ActionEvent arg0) {

                patientname = textField.getText(); // Getting the patient name from the textfield
                patientaddress = textField_1.getText();
                patientphone = textField_2.getText();


                textField.setText(""); // Setting the textfield to be blank so that the user can input there name address etc..
                textField_1.setText("");
                textField_2.setText("");

                patientid = patientid + 1;//Implementing the id to add 1 every time another id is added

                Patient patient = new Patient(patientname, patientaddress, patientphone, patientid); // Populating the array list patientlist with the users input
                patientList.add(patient); // Adding the patient's details to the patientlist

                patientListModel.addElement(patient); // adds the patient's details to the list model


            }

            public void onSave(List<Patient> PatientList) {
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new FileOutputStream(new File("PatientList.ser")));
        out.writeObject(PatientList);
        out.flush();
    }
    catch (IOException e) {
        // handle exception
    }
    finally {
        if (out != null) {
            try {
                out.close();
            }
            catch (IOException e) {
                // handle exception
            }
        }
    }
}

public List<Patient> onLoad() {
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new FileInputStream(new File("PatientList.ser")));
        return (List<Patient>) in.readObject();
    }
    catch (IOException e) {
        // handle exception
    }
    catch (ClassNotFoundException e) {
        // handle exception
    }
    finally {
        if (in != null) {
            try {
                in.close();
            }
            catch (IOException e) {
                // handle exception
            }
        }
    }
    return null;
}



        });


        btnAdd.setBounds(0, 86, 65, 23);
        contentPane.add(btnAdd);

/////////////////

Here is my patient class :

public class Patient {


    public String patientName;
    public String patientAddress;
    public String patientPhone;
    public int patientID;




    public Patient(String patientname, String patientaddress, String patientphone,int patientid){


        patientName = patientname;
        patientAddress = patientaddress;
        patientPhone = patientphone;
        patientID = patientid;


    }



    public String setName(String patientname){

        return patientName = patientname;



    }

    public String getName(){

        return patientName;

    }

    public String setAddress(String patientaddress){

        return patientAddress = patientaddress;


    }

    public String getAddress(){

        return patientAddress;
    }



    public String setPhoneNum(String patientphone){
        return patientPhone = patientphone;

    }

    public String getPhoneNum(){

        return patientPhone;
    }

 public int setID(int patientid){

        return patientID = patientid;



    }

    public int getID(){

        return patientID;

    }




    public String toString() { // Printing the patient's details to the scroll pane
        return "Patient Name: " + patientName + ", PatientAddress: "
                + patientAddress + ", PatientPhone: " + patientPhone
                + ", patientID: " + patientID +"" ;
    }

}
Shane Reen
  • 21
  • 5
  • What errors are you receiving? The code for the `Patient` class might be of interest also. – Kevin Apr 27 '13 at 10:23
  • I receive no error. But it never created a serialisation file and when the program is closed , the previous entries to the jlists arent loaded. I will add my patient class to the question now – Shane Reen Apr 27 '13 at 10:28
  • Are you sure that `onsave` is called? It is unusual for it to not work without throwing an exception. I assume it is a typo, but your `Patient` class needs to implement `Serializable`. Print a message in the catch block (or just remove it, as it could be smothering any error you receive). – Kevin Apr 27 '13 at 10:47
  • I am not sure. I don't think it is being called. I tried to call it but i get an error message when it is tryin to be called. – Shane Reen Apr 27 '13 at 11:01
  • What is the error message? – Kevin Apr 27 '13 at 11:02
  • It keeps asking me to create an onSave method but i've it created already. I've realised it's not calling the method but I dont't understand how it can't recognise the method is already created! – Shane Reen Apr 27 '13 at 11:12
  • Where is `onsave` being called? See here for how to carry out operations when a JFrame closes: http://stackoverflow.com/questions/9093448/do-something-when-the-close-button-is-clicked-on-a-jframe – Kevin Apr 27 '13 at 11:18

0 Answers0