在java中保留对话框的数据 [英] to persist the data of a dialog box in java

查看:305
本文介绍了在java中保留对话框的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我创建了一个 JDialog 其中有一张桌子。我可以通过单击按钮将表数据保存到文件中,但我想要做的是将数据保留在表中,以便在下次运行程序时,该数据可用并显示在表中按下确认按钮时。虽然我读到了Java持久性和Java序列化概念,但我并没有清楚地了解哪种技术适合以及如何使用这个问题。

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.

推荐答案

考虑使用 java.util.Preferences 保存 TableModel 数据,例如 ,或 javax.jnlp.PersistenceService ,引用其他

Consider saving just the TableModel data using java.util.Preferences, exemplified here, or javax.jnlp.PersistenceService, cited here.

这篇关于在java中保留对话框的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆