Hi everyone,
I am trying to save the entire jtable (as an object if possible) to my disk and then be able to read that same file in the same table.

First there are two buttons on my frame one is "load" and the other "save as"

when the save as button is clicked i need the entire table with all its contents to be saved to the disk.

Now here's my problem in that after i have deleted all the columns and rows in my jtable and try to load the previously saved file nothing happens(and yes i have add an action listener for the button). What i need is that when the entire jtable is empty is for the jtable to load that file with their columns and data intact or as the way they were saved.

Can anyone tell me how this can be done or show me any sample program that saves and loads the jtable.

I am using the DefaultTableModel class as my table model and Table1 is the instance of my JTable.

the function i use for saving the contents is shown below

public void tablesave ()
{

if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
{

}

try
{
File f = FileChooser1.getSelectedFile();
FileOutputStream fStream = new FileOutputStream(f);
ObjectOutput stream = new ObjectOutputStream(fStream);
stream.writeObject(Table1.getModel());
stream.flush();
stream.close();
fStream.close();
}

catch (IOException e)
{
Label1.setText("A table saving error has occurred");
}

}

the function i use for loading the contents is shown below

public void tableload ()
{

if(FileChooser2.showOpenDialog(fr) != JFileChooser.APPROVE_OPTION)
{

}

try
{
File f = FileChooser2.getSelectedFile();
FileInputStream fStream = new FileInputStream(f);
ObjectInput stream = new ObjectInputStream(fStream);
Object obj = stream.readObject();
if(obj instanceof TableModel)
{
Table1.setModel((TableModel) obj);
}
stream.close();
fStream.close();
}

catch (Exception e)
{
Label1.setText("A table loading error has occurred");
}

}

I really hope someone can help me with this

Thank You

Yours Sincerely

Richard West

how is the table data represented? Is it from a Vector? To make things simple, you could just save the Vector by use of serialization. When you change the table's data and/or column headers, you have to let the table know that the data has changed by firing a trigger event. I can't remember if this is the correct method or not: fireTableDataChanged()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.