Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I need help with saving state data.

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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"

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    JonLane (February 26th, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: I need help with saving state data.

    Quote Originally Posted by Sean4u View Post
    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.
    Last edited by JonLane; February 27th, 2012 at 03:10 AM. Reason: typing in dark/typos

  5. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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!

  6. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I need help with saving state data.

    Quote Originally Posted by JonLane View Post
    ...
    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.

  7. The Following User Says Thank You to Sean4u For This Useful Post:

    JonLane (February 27th, 2012)

  8. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I need help with saving state data.

    Quote Originally Posted by JonLane View Post
    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

  9. The Following User Says Thank You to Sean4u For This Useful Post:

    JonLane (February 27th, 2012)

  10. #7
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: I need help with saving state data.

    moving questions to Data base section!
    Last edited by JonLane; February 27th, 2012 at 04:25 AM.

Similar Threads

  1. Saving the dialog box data
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 24th, 2011, 02:53 PM
  2. saving data in persian language in mysql db
    By java_cs in forum JDBC & Databases
    Replies: 2
    Last Post: January 19th, 2011, 12:26 PM
  3. saving the data of the child jsp into parent jsp
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 15th, 2011, 11:05 AM
  4. saving list of data from JSP into java object
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: January 12th, 2011, 11:24 AM
  5. need help with saving data
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2010, 03:33 PM