| | |
Saving and opeing a JTable
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
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
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
•
•
Join Date: Mar 2004
Posts: 773
Reputation:
Solved Threads: 38
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()
![]() |
Similar Threads
- jTable and File Saving (Java)
Other Threads in the Java Forum
- Previous Thread: StringTokenizer
- Next Thread: stdin.readlin() doesn't want to work ?
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component csv database desktop draw ebook eclipse equation error event exception fractal game givemetehcodez graphics gui html ide image input integer intersect iphone j2me java java.xls javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions reporting rotatetext scanner screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





