hey guys,
I'm making a small app that make a Jable and saves the table model then loads the table model and set it to the table interface and both saving and loading is in seperate frames.
// Saving the info
String ufname = Fname.getText();
String ulname = Lname.getText();
String ubalance = balance.getText();
String cdate = datetxt.getText();
if (ufname.equals("") || ulname.equals("") || ubalance.equals("")) {
final JPanel panel = new JPanel();
JOptionPane.showMessageDialog(panel, "Entries Empty! Fill in the required entries.", "Error",
JOptionPane.ERROR_MESSAGE);
} else {
//making a saving table
String[] header = {"First Name", "Last Name", "Balance", "Data"};
String[][] elements = {
{ufname, ulname, ubalance, cdate}
};
String filename = "table.file";
JTable table = new JTable(elements, header);
TableModel tm = table.getModel();
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(tm);
oos.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
final JPanel panel2 = new JPanel();
JOptionPane.showMessageDialog(panel2, "Data Has Been Saved!", "SAVED",
JOptionPane.HEIGHT);
setVisible(false);
}
//NOT this code is in NetBeans GUI Builder so it looks a little bit funky
and about my code in loading is
String filename = "table.file";
TableModel tm;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
tm = (TableModel) ois.readObject();
}
catch(Exception e) {
e.printStackTrace();
return;
}
tabledis.setModel(tm);
Hope I get some help here.