我无法在swing项目中保存表格的变化

时间:2016-03-30 10:04:21

标签: java swing properties preference savechanges

我更改了表格的行,然后保存。但当我再次关闭并打开项目时,它会按照自己的意愿设计桌子。有关于保存摇摆项目变化的想法吗?任何技术,方法?这是我的保存方法。

  

public void saveLayoutData(String prePath){

    Properties pMain = new Properties();
    File dirMain = new File(prePath + "DWorkSpacesAll" + ".xml");
    File dirPath = new File(prePath);
    if (!dirPath.exists()) {
        File dirBase = new File("./Preferences");
        dirBase.mkdir();
        dirPath.mkdir();
    }
    if (!dirMain.exists()) {
        try {
            dirMain.createNewFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    pMain.put("ShowConfirmationDialog", showConfirmation.toString());
    for (DWorkspace w : workSpaces) {
        try {
            w.setTitle(tabbedPane.getTitleAt(workSpaces.indexOf(w)));
        } catch (Exception e) {
            continue;
        }

        pMain.put(w.getPreferenceKey(), w.getTitle());
        File dir = new File(prePath + w.getPreferenceKey() + ".dat");


        try {
            if (!dirPath.exists()) {
                File dirBase = new File("./Preferences");
                dirBase.mkdir();
                dirPath.mkdir();
            }
            if (!dir.exists()) {
                dir.createNewFile();
            }
            w.getDockingManager().
                    saveLayoutDataToFile(dir.getCanonicalPath());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    FileOutputStream fosMain = null;
    try {
        fosMain = new FileOutputStream(dirMain.getCanonicalPath());
        pMain.storeToXML(fosMain, "");
        fosMain.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (fosMain != null) {
                fosMain.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    try {

        for (DWorkspace w : workSpaces) {
            File dir = new File(prePath + w.getPreferenceKey() + ".xml");

            Properties p = new Properties();

            for (DIComponent c : w.getComponents()) {

                if (c instanceof DIWithPreferences) {

                    DIWithPreferences cwp = (DIWithPreferences) c;

                    String pref = cwp.getPreferences();

                    if (pref != null) {
                        p.put("_" + w.getPreferenceKey() + "_" + cwp.getKey(), pref);
                    }

                }
            }

            p.put("_" + w.getPreferenceKey() + "_" + "Table1", w.Table1());

            p.put("_" + w.getPreferenceKey() + "_" + "Table2",
                    w.Table2());

            p.put("_" + w.getPreferenceKey() + "_" + "Table3",
                    w.Table3());

            p.put("_" + w.getPreferenceKey() + "_" + "Table4",
                    w.Table4());

            if (w.getPreferenceKey() == null) {
            } else {
                p.put("_" + w.getPreferenceKey() + "_" + "Table5",
                        w.Table5());
            }

            if (w.getPreferenceKey() == null) {
            } else {
                p.put("_" + w.getPreferenceKey() + "_" + "Table6",
                        w.Table6());
            }

            FileOutputStream fos = new FileOutputStream(dir.getCanonicalPath());
            p.storeToXML(fos, "");
            fos.close();

        }
    } catch (Exception ex) {
        log.error("", ex);
    }
}

0 个答案:

没有答案