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();
}
}
}