if(e.getActionCommand().equals("save to file"))
{
System.out.println("save is pressed");
StringBuffer fileContent = new StringBuffer();
TableModel tModel = m_table.getModel();
for (int i = 1; i < tModel.getRowCount(); i++)
{
for(int j=0;j<tModel.getColumnCount();j++)
{
Object cellValue = tModel.getValueAt(i, j);
// ... continue to read each cell in a row
fileContent.append(cellValue);
// ... continue to append each cell value
fileContent.append(" ");
}
fileContent.append("\n");
}
FileWriter fileWriter;
try {
fileWriter = new FileWriter(new File("data.txt"));
fileWriter.write(fileContent.toString());
fileWriter.flush();
fileWriter.close();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
I have created a JDialog
in which there is a table. I am able to save the table data into the file on a button click, but what i want to do is to keep that data persisted in the table so when the next time the program is run, that data is available and displayed in the table when a confirm button is pressed. Though I read about Java persistence and Java serialization concepts, I'm not getting the clear idea which technique is appropriate and how to use for this problem.
It depends on your application. The easiest and quickest way is object serialization. You can save an object into to a file and load it again. You would do this with the object that holds your table data. There are a lot of tutorials available for that.
You can extend the concept of serialization by transforming your object in something human readable first (like XML or JSON) and save it as a file then. This has the benefit that you can use the exported data in something other than a Java program. A framework for marshalling to XML is JAXB. Also a lot of tutorials on that.
Finally you could use a SQL database. A database would be the most performant alternative. A nice framework for persistence in SQL ins Hibernate. It is mostly used for bigger business applications that need a fast data storage backend. Of cause you have to run an SQL server for that one. Again a lot of tutorials available.
I guess for your case the first alternative would be the best because its quick and easy. SQL would be a little overkill ;-)
If you don't have access to a database (local or on a server), the best way will probably to use the Serialization.
Consider using a concrete class to populate your JTable
. So to save the data, you just have to make this class to implement Serializable
, and then write it in a file using an ObjectOutputStream
. Then to retrieve it later use an ObjectInputStream
.
I don't really remember how JTable
works with data, but you will probably have to create a custom Model class that inerhits from AbstractTableModel
, and will contain the data whether in an array or in a List
. But it's better to use a List
since it is a lot more fexible and easy to use. So you can direclty serialize the List, and retrieve when program run next time.
来源:https://stackoverflow.com/questions/13306688/to-persist-the-data-of-a-dialog-box-in-java