Need help using SingleFrameApplication to save sessions
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.
精彩评论