0

In this code, I'm trying to read data from the bin file and set it in the table view column. But I can't set the loop properly in loadTableFromFileButtonOnClick.It is only showing the latest value from the bin file. But I want to load all the binding data from the bin file Here is my controller class code.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

public class FXMLMainSceneController implements Initializable {
    
    @FXML private TextField idTxt;
    @FXML private TextField nameTxt;
    @FXML    private TextField deptTxt;
    @FXML private TextField cgpaTxt;
    @FXML private DatePicker birthdayDatePicker;
    
    @FXML private TableView<Student> tableView;
    @FXML    private TableColumn<Student, String> idColumn;
    @FXML    private TableColumn<Student, String> nameColumn;
    @FXML    private TableColumn<Student, LocalDate> birthdayColumn;
    @FXML    private TableColumn<Student, String> deptColumn;
    @FXML    private TableColumn<Student, String> cgpaColumn; 

    @Override
    public void initialize(URL url, ResourceBundle rb) {       
        idColumn.setCellValueFactory(new PropertyValueFactory<Student,String>("id"));
        nameColumn.setCellValueFactory(new PropertyValueFactory<Student,String>("name"));
        birthdayColumn.setCellValueFactory(new PropertyValueFactory<Student,LocalDate>("birthday"));
        deptColumn.setCellValueFactory(new PropertyValueFactory<Student,String>("dept"));
        cgpaColumn.setCellValueFactory(new PropertyValueFactory<Student,String>("cgpa"));
    }    

    @FXML
    private void saveToFileButtonOnClick(ActionEvent event) {             
        Student stud = new Student(  
                    Integer.parseInt(idTxt.getText()),
                    nameTxt.getText(),
                    birthdayDatePicker.getValue(),
                    deptTxt.getText(),
                    Float.parseFloat(cgpaTxt.getText())  
                );
        idTxt.setText(null);    nameTxt.setText(null);  cgpaTxt.setText(null);  deptTxt.setText(null);
        stud.display();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Stud.bin"));
            oos.writeObject(stud);
            oos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
     
 

    @FXML
    private void loadTableFromFileButtonOnClick(ActionEvent event) {
        ObjectInputStream ois=null;
         try {
            Student s;
            //There will be a loop for set up all the data ,i tried bt can't apply it properly
            ois = new ObjectInputStream(new FileInputStream("Stud.bin"));
            s = (Student) ois.readObject();
            s.display();
            tableView.getItems().add(s);
            
        } catch (Exception ex) {
            try {
                if(ois!=null)
                    ois.close();
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            ex.printStackTrace();
        }        
    }

    @FXML
    private void idTxtOnMouseClick(MouseEvent event) {
        idTxt.setText(null);
    }
    @FXML
    private void nameTxtOnMouseClick(MouseEvent event) {
        nameTxt.setText(null);
    }
    @FXML
    private void cgpaTxtOnMouseClick(MouseEvent event) {
        cgpaTxt.setText(null);
    }

    @FXML
    private void deptTxtOnMouseClick(MouseEvent event) {
        deptTxt.setText(null);
    }
    
}

Here is my subclass

import java.io.Serializable;
import java.time.LocalDate;

public class Student extends Person implements Serializable{  
    int id;  
    String dept;
    float cgpa;
    public Student(int id, String name, LocalDate birthday, String dept, float cgpa) {  
        super(name, birthday);
        this.id = id;  
        this.dept = dept;
        this.cgpa = cgpa;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }
    
    public void setCgpa(float cgpa) {
        this.cgpa = cgpa;
    }
        
    public int getId() {
        return id;
    }

    public String getDept() {
        return dept;
    }
    
    public float getCgpa() {
        return cgpa;
    }
    
    @Override
    public String toString(){
        return "Id="+id+", Name="+name+", DoB="+birthday+", Dept="+dept+", Cgpa="+cgpa;
    }
    
    public void display(){
        System.out.println("Id="+id+", Name="+name+", DoB="+birthday+", Dept="+dept+", Cgpa="+cgpa);
    }
} 

here is my superclass

import java.io.Serializable;
import java.time.LocalDate;
import javafx.beans.property.SimpleStringProperty;

public class Person implements Serializable{
    protected String name;
    protected LocalDate birthday;

    public Person(String name, LocalDate birthday) {
        this.name = name;
        this.birthday = birthday;
    }

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

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    } 
    
    public String getName() {
        //return firstName;
        return name;
    }

    public LocalDate getBirthday() {
        return birthday;
    }
}
  • 1
    check this link https://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream – Alex May 04 '21 at 09:19
  • same request as for your previous question: [mcve] please - mind the _M_! that is strip it down to a single problem to solve. Here's it's unclear whether it's the reading (unrelated to javafx, provide an example without ui) or the adding (then the reading is unrelated, replace it with adding hard-coded data). Note that the problem needs to be _reproducible_ - if it involves the ui loaded via fxml, it must contain application, controller, a data class (one is enough) and fxml to load the ui. – kleopatra May 04 '21 at 09:46
  • seems indeed to be the first (saving/reading multiple objects to/from the stream), voting to close as duplicate of the question referenced by @Alex – kleopatra May 04 '21 at 09:55
  • 1
    Does this answer your question? [Appending to an ObjectOutputStream](https://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream) – Alex May 04 '21 at 11:27

0 Answers0