1

I am working on tutorial software which help to solve problems of a Financial Book. So it is something like an worksheet kind of thing containing combo-boxes and formatted text boxes. Everything is working as i want except one thing. Is there a way i could save my work done by me any point of time.

Lemme be specific. There are twelve chapters and each chapter contains some 15-20 problems. Each problem is design with combination of text boxes and combo-boxes and some jtabbed panel etc. If i solved half my worksheet and want to save so that i could open it later i open my software. Is there any way to do that. The problem is every sheet have different no of combo-box and text boxes so the variable are also different.

Thanks in advance

Gaurav
  • 55
  • 10
  • If this is a NetBeans [Java Desktop Application](http://stackoverflow.com/questions/2561480/netbeans-gui-editor-generating-its-own-incomprehensible-code/2561540#2561540), consider adding the `jsr296` tag. – trashgod Jun 25 '11 at 19:37

2 Answers2

1

Well you must have then a persistent store in order to save the state of this desktop application.Think on a database or file store. You can also serialize "the state of the app." in an object and save it on disk and when you start the app again you reload it.

Cris
  • 4,947
  • 6
  • 44
  • 73
  • I second this. It doesn't seem to warrant the overhead of a database, you could probably get away with just writing your values into a file, then parsing them back into the program the next time you load it. – Andrew Jun 25 '11 at 18:44
  • true ..but i assume the questions for the tutorial ,might come from database (local one derby for example) and then he will have a more flexible app , not used only with some hard-coded questions. – Cris Jun 25 '11 at 18:48
  • Thanks for the help but can you elaborate some more. I should inform you some more. Suppose i have a jPanel containing 5 combo-box and 5 Formatted text box. combo-box have a list. I entered something in 3 of them. I want to save my work till here. So that the next time i open. All those 3 combo and text box should contain the same think which i had selected and typed in them. – Gaurav Jun 27 '11 at 19:06
  • no question are not from database. They are fixed according to a reference book. so they are hard coded – Gaurav Jun 28 '11 at 13:11
1

You need a model for your worksheet. This model is a data-structure and has nothing (per se) to do with the fact that GUI widgets are used to represent it.

You save the state of the model -- start with simple Serialization -- to disk on save. Load on startup.

Something along these lines allow for uniform treatment of your worksheets.

public interface PersistentModel {
    boolean save (OutputStream out) throws IOException;
    boolean load (InputStream out) throws IOException;
}
public interface SpreadSheet extends PersistentModel {
    Worksheet[] getWorksheets();
    Worksheet getWorksheet(int i); // and other related, etc.
}

public interface Worksheet extends PersistentModel {
   int id();
   Element get(String name):
   Element[] getElements();

   public interface Element<T> extends PersistentModel { 
        String getName();
        T getValue();
        boolean setValue(T v);
   }
}

Swing uses MVC and you have standard mechanisms of hooking your model (the data structure holding state of the worksheets), and, the GUI (swing likely) that is the presentation of the same. I assume that is not an issue.

alphazero
  • 27,094
  • 3
  • 30
  • 26
  • Thanks for this reply. I havent got exactly what you mean. Can you explain. I would like to ask mt question again.Suppose i have a jPanel containing 5 combo-box and 5 Formatted text box. combo-box have a list. I entered something in 3 of them. I want to save my work till here. So that the next time i open. All those 3 combo and text box should contain the same think which i had selected and typed in them – Gaurav Jun 27 '11 at 19:07
  • Your view -- a JPanel in this case -- presents the 'model' data so a worksheet maps to a JPanel with bunch of controls. So you will need a way to set jpanel state from the model and to update the model when the view changes. That's standard GUI programming. Above answer is a suggested structure for a uniform model to use. – alphazero Jun 27 '11 at 20:38