I need help with saving state data.
I have a gui, and the gui is effectively made up of a bunch(100+) JPanels.
my panels are moveable, and I want to be able to save a current state, and load it.
The code needs some cleaning up before I post it, but I wanted to get the ball rolling on learning how to save things more complicated then text, as well as loading them.
I also dont even know how to create a java executable! (currently I use TextPad, and compile and run from there)
never done a build.
Thanks for any help,
Jonathan
Re: I need help with saving state data.
You'd have to say how complicated your 'state' is before you'd get any really useful replies. java.util.prefs.Preferences gives you a Key-Value store to keep state in between runs of your program - the actual storage for which is magic, you don't need to say (or know) where the data is kept. You can store object hierarchies to streams (including to/from files on disk) with the Serializable and Externalizable interfaces in java.io. The gotcha with those is that you may be unable to read old data - at all - if you change the structure of your classes. I made a little project at google code - JuppMap - which does a bit of both: it's like Preferences but you can store any Serializable object in it. I don't think it has ever had a user, so caveat emptor.
As for 'executable' the simplest way is to make a jar out of your classes:
Creating a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
Make a JAR executable - Real's Java How-to
If you want to deploy over the web, look up JNLP / "Java Web Start"
Re: I need help with saving state data.
Quote:
Originally Posted by
Sean4u
You'd have to say how complicated your 'state' is before you'd get any really useful replies. java.util.prefs.Preferences gives you a Key-Value store to keep state in between runs of your program - the actual storage for which is magic, you don't need to say (or know) where the data is kept. You can store object hierarchies to streams (including to/from files on disk) with the Serializable and Externalizable interfaces in java.io. The gotcha with those is that you may be unable to read old data - at all - if you change the structure of your classes. I made a little project at google code - JuppMap - which does a bit of both: it's like Preferences but you can store any Serializable object in it. I don't think it has ever had a user, so caveat emptor.
As for 'executable' the simplest way is to make a jar out of your classes:
Creating a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
Make a JAR executable - Real's Java How-to
If you want to deploy over the web, look up JNLP / "Java Web Start"
Alright, good starting point, Ill research prefs library and with luck it will not be as spread out as GUI libraries.
to be more specific, I am looking to store an entire Object, most of the data inside these objects is from the AWT/Swing library, and just creating panels and components in specific layouts. I certainly would not mind my program already knowing what kind of objects these are upon loading, but I have no idea how it works. There will be the occasional graphic I want to use, that will be as complicated as say a JPEG with a picture of someones face in it. these idealy will be stored with their objects that use them, though if i have to, I can rebuild my objects once the program loads them.
keep up the input, much appreciated Sean
Jonathan
Edit: I have reviewed my code, and my most complicated state is a centerGrid Object it has a few nested JPanels using LayoutManagers to get the desired layout. in the deepest layout it is filled with sometimes as many as 2500 JPanels all using card Layout to hold maximum 3 more objects, but will be 70 percent 1 object. these objects are customized JPanels with listeners and some class variables.
I still am reading and absorbing the resource lead you gave me, so I don't know how states work, but I am not against rebuilding the state at run time by loading individual tiles.
Re: I need help with saving state data.
How does preferences delete magically stored data, I have been testing preferences and don't want to fill up my memory with useless data! I am sure this isn't a risk knowing how java works in other areas, but still better to be safe then sorry!
Re: I need help with saving state data.
Quote:
Originally Posted by
JonLane
...
If storing / retrieving data is an integral part of your application and you have many data objects to store that are as complicated as you're suggesting, you may be better off skipping any trivial persistence methods like java.util.prefs.Preferences and going directly for a full-blown database. Unless - would your application have a natural 'document', so that someone using your application might work on a document for some time before attaching it to an email and sending it to someone else who would also use your application to open and work on the document? Serializable / Externalizable might still be best in that case, or a mix of XML and images in a compressed file archive is also an option. It'd be good to think all the use cases through first before picking a save method.
Re: I need help with saving state data.
Quote:
Originally Posted by
JonLane
How does preferences delete
See the methods starting 'clear' and 'remove'. On my Linux PCs, entries are removed from XML files or the files themselves are removed, or the directory tree containing the XML files are removed. I'm not sure exactly what happens on Windows when you use clear/remove
Re: I need help with saving state data.
moving questions to Data base section!