开发者

Need help using SingleFrameApplication to save sessions

开发者 https://www.devze.com 2023-03-21 17:39 出处:网络
I have a program that need to save all of the things in JTextFields, JComboBoxes, etc. I came upon an example that lead me to believe i could achieve this with the SingleFrameApplication class.

I have a program that need to save all of the things in JTextFields, JComboBoxes, etc.

I came upon an example that lead me to believe i could achieve this with the SingleFrameApplication class.

There are 1000+ components in this program that would need to be kept开发者_如何学运维 track of if serializing.

Here is what i have so far:

public class NewMain extends SingleFrameApplication{

    //the file for the session to be saved to
    String sessionFile = "sessionState.xml";
    //Container is a class I made that extends a JPanel
    Container container;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    Application.launch(NewMain.class, args);
    }

    @Override
    protected void startup() {
        try {
            //load the file
            getContext().getSessionStorage().restore(getMainFrame(), sessionFile);
            container = new Container(getContext());
            show(container);

        }
        catch (Exception e) {

        }
    }

    @Override
    protected void shutdown() {
        try {
            //save to the file so we can open it later
            getContext().getSessionStorage().save(getMainFrame(), sessionFile);
        }
        catch (Exception e) {

        }
    }
}

When ever I open run the .jar file and change some values in JTextFields, JComboBoxes, etc. and then close the program and reopen it, the data was not saved. Can anyone explain why this isnt working or make some suggestions for what i need to do differently? Thank you.


I would suggest you use serialization for this situation. check out this: http://java.sun.com/developer/technicalArticles/Programming/serialization/ or: http://www.java2s.com/Tutorial/Java/0180__File/Savingandrestoringthestateofclasses.htm

Not all objects are serializable but as it says in the first link, "...Swing GUI components, strings, and arrays -- are serializable" and you can write your own class that implements the serializable interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号