2

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.

Michael Rentmeister
  • 167
  • 1
  • 6
  • 24

1 Answers1

1

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.

Ido Weinstein
  • 1,176
  • 8
  • 24
  • I really don't see how this could help me exactly, could you please explain what serialization is and how i would go about doing it in this instance? – Michael Rentmeister Jul 18 '11 at 19:19
  • again, from the first link... "Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time." you want to serialize all of the stuff in your container, and these are swing components as I understand, so what you need to do is serialize them into a file and deserialize them when you want them again. – Ido Weinstein Jul 18 '11 at 19:25
  • serialize all the stuff you want at shutdown and deserialize at initialize or startup – Ido Weinstein Jul 18 '11 at 19:33
  • The problem with serializing all of the components is the Container JPanel contains a JTabbedPane that Contains 10 more JPanels. There are 1000+ components in this program, it's not a small program, so serializing all of them would be inefficient wouldn't it? – Michael Rentmeister Jul 18 '11 at 19:34
  • Efficiency will be achieved if you only serialize-deserialize only components you need and do it as less as you can. Performance wise it could be problematic too, so you need to think this over and maybe run some tests to see if it fits your needs. – Ido Weinstein Jul 19 '11 at 06:24
  • Could you possibly suggest something else then? – Michael Rentmeister Jul 20 '11 at 13:39