-1

How do I store a TableView Object to a file, then read the file and recreate the original TableView Object? When I execute the program, I get this value [myPackage.Entry@1fdda87c] which I assume is the ID of the TableView, or the ID of a row/column inside the TableView. But I want the value of the rows and column inside the TableView instead. I'm not using SQL for my application, and im saving it as a txt file.

Controller class:

import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Screen;

      public class MyController implements Serializable {
    
        @FXML
    
      public  static TableView  tab = new TableView ();

    TableColumn UsernameColumn = new TableColumn("username");

    TableColumn TitelColumn = new TableColumn("titel");

    TableColumn URLColumn = new TableColumn("url");

    TableColumn PasswordColumn = new TableColumn("password");

    TableColumn NotesColumn = new TableColumn("notes");

  @FXML
    void initialize() {
        UsernameColumn.setCellValueFactory(new PropertyValueFactory<>("username"));
        TitelColumn.setCellValueFactory(new PropertyValueFactory<>("titel"));
        URLColumn.setCellValueFactory(new PropertyValueFactory<>("url"));
        PasswordColumn.setCellValueFactory(new PropertyValueFactory<>("password"));
        NotesColumn.setCellValueFactory(new PropertyValueFactory<>("notes"));
        tab.getColumns().addAll(TitelColumn,UsernameColumn,URLColumn,PasswordColumn,NotesColumn);
}

My Entity Class

import java.io.Serializable;

import javafx.scene.control.TextField;

public class Entry implements Serializable {
    Entry(String titelString, String usernameString, String urlString, String passwordString,String noteString) {
        this.titel=new TextField(titelString);
        this.username=new TextField(usernameString);
        this.url=new TextField(urlString);
        this.password=new TextField(passwordString);
        this.notes=new TextField(noteString);

    }
    TextField titel, username, url, password, notes;
    public TextField getTitel() {
        return titel;
    }

    public TextField getUsername() {
        return username;
    }

    public TextField getUrl() {
        return url;
    }

    public TextField getPassword() {
        return password;
    }

    public TextField getNotes() {
        return notes;
    }


}

Adding new entities

public class ObjectsFXML  implements Serializable  {
      void entryRows  ()
            {
                Entry entry = new Entry("test", "john","www","test","notes");
                myController.tableView.getItems().add(entry);
                File file = new File (myFileName);
                write(MyController.tab.getItems(),file);
        
            }

    private static void write(ObservableList<Entry> entryObservableList, File file) {
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
    
              for (Entry entry : entryObservableList) {
              bufferedWriter.write(entry.toString());
              bufferedWriter.newLine();
              }
                System.out.println(entryObservableList);
              bufferedWriter.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
}
romes
  • 5
  • 4
  • 1
    You need to determine how you want to write the data to the file. JSON, XML, CSV, TSV, etc. I personally prefer writing to an `SQLite` database. – SedJ601 Apr 05 '22 at 03:12
  • i want to write it to a txt or CSV file. – romes Apr 05 '22 at 10:42
  • 2
    Not related to your question, but I don't understand why you store TextFields in the Entry class instead of Strings or StringProperties. The Entry appears to be immutable so you could use a [record](https://stackoverflow.com/questions/70175587/how-do-you-use-a-javafx-tableview-with-java-records). Also, prefer lambdas to PropertyValueFactory, see the linked answer for more info. For storing data as XML, which is a structured text file, you can see the [makery tutorial](https://code.makery.ch/library/javafx-tutorial/part5/). – jewelsea Apr 05 '22 at 17:14
  • I [added an answer to the duplicate](https://stackoverflow.com/a/71772536/1155209) which demonstrates saving and restoring data which backs a TableView using a json file. The serialization/deserialization mechanism uses the 3rd party Jackson library. – jewelsea Apr 06 '22 at 19:28

1 Answers1

-2

I got the solution for this by following the instructions here https://stackoverflow.com/a/52129734/18424338

and if your entity object getters is of type TextField, use TextField.getText() to get the text value inside the TextField object

romes
  • 5
  • 4