I am creating an application which helps solving problems of an accounting book. The application has 12 chapters. All chapters contains 15-20 problems. The problem is displayed in a JPanel containing various combo-boxes and formatted-text boxes. suppose i solved a problem half and want to save so that next time i can load that half solved question.
Saving should be done by clicking save from menubar. And loading by load from the menubar
All menubars and problem sheet are working but i am not able to save any thing. I was using jFilechooser...
Is their any way to do that?
How to save a panel with filled combo-boxes items and text-boxes. And is their any way to know that user has made any changes in any items so that on closing the problem i can again ask to save it first and then exit.
Thanks in advance.
Some of my codes:
private void openBtnMouseClicked(java.awt.event.MouseEvent evt) {
opening();
}
public void opening() {
JFileChooser chooser=new JFileChooser();
int choice=chooser.showOpenDialog(this);
javax.swing.JComboBox[] sourceALE = {aaCombo, baCombo, caCombo, daCombo, eaCombo, faCombo, gaCombo, haCombo, iaCombo, jaCombo, kaCombo,
alCombo, blCombo, clCombo, dlCombo, elCombo, flCombo, glCombo, hlCombo, ilCombo, jlCombo, klCombo,
aeCombo, beCombo, ceCombo, deCombo, eeCombo, feCombo, geCombo, heCombo, ieCombo, jeCombo, keCombo};
javax.swing.JTextField[] sourceP = {aeval1, beval, ceval, deval, eeval, feval, geval, heval, ieval, jeval, keval};
String [] comboboxes={"aaCombo", "baCombo", "caCombo", "daCombo", "eaCombo", "faCombo", "gaCombo", "haCombo", "iaCombo", "jaCombo", "kaCombo","alCombo", "blCombo", "clCombo", "dlCombo", "elCombo", "flCombo", "glCombo", "hlCombo", "ilCombo", "jlCombo","klCombo","aeCombo", "beCombo", "ceCombo", "deCombo", "eeCombo", "feCombo", "geCombo", "heCombo", "ieCombo", "jeCombo", "keCombo"};
if(choice==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
System.out.println("Hey");
Scanner scanner = new Scanner(new FileReader(file));
while ( scanner.hasNextLine() ){
Scanner scan = new Scanner(scanner.nextLine());
scan.useDelimiter("=");
if ( scan.hasNext() ){
String item=scan.next();
int value=scan.nextInt();
String color=scan.next();
for(int g=0;g<comboboxes.length;g++){
if(item.equals(comboboxes[g])) {
if(value<3)
sourceALE[g].setSelectedIndex(value);
if(color.equals("red"))
sourceALE[g].setForeground(red);
if(color.equals("green"))
sourceALE[g].setForeground(green);
if(color.equals("blah"))
sourceALE[g].setForeground(blah);
}
}
}
scan.close();
}
scanner.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(q1.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void opening() {
JFileChooser chooser=new JFileChooser();
int choice=chooser.showOpenDialog(this);
javax.swing.JComboBox[] sourceALE = {aaCombo, baCombo, caCombo, daCombo, eaCombo, faCombo, gaCombo, haCombo, iaCombo, jaCombo, kaCombo,
alCombo, blCombo, clCombo, dlCombo, elCombo, flCombo, glCombo, hlCombo, ilCombo, jlCombo, klCombo,
aeCombo, beCombo, ceCombo, deCombo, eeCombo, feCombo, geCombo, heCombo, ieCombo, jeCombo, keCombo};
javax.swing.JTextField[] sourceP = {aeval1, beval, ceval, deval, eeval, feval, geval, heval, ieval, jeval, keval};
String [] comboboxes={"aaCombo", "baCombo", "caCombo", "daCombo", "eaCombo", "faCombo", "gaCombo", "haCombo", "iaCombo", "jaCombo", "kaCombo","alCombo", "blCombo", "clCombo", "dlCombo", "elCombo", "flCombo", "glCombo", "hlCombo", "ilCombo", "jlCombo","klCombo","aeCombo", "beCombo", "ceCombo", "deCombo", "eeCombo", "feCombo", "geCombo", "heCombo", "ieCombo", "jeCombo", "keCombo"};
if(choice==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
System.out.println("Hey");
Scanner scanner = new Scanner(new FileReader(file));
while ( scanner.hasNextLine() ){
Scanner scan = new Scanner(scanner.nextLine());
scan.useDelimiter("=");
if ( scan.hasNext() ){
String item=scan.next();
int value=scan.nextInt();
String color=scan.next();
for(int g=0;g<comboboxes.length;g++){
if(item.equals(comboboxes[g])) {
if(value<3)
sourceALE[g].setSelectedIndex(value);
if(color.equals("red"))
sourceALE[g].setForeground(red);
if(color.equals("green"))
sourceALE[g].setForeground(green);
if(color.equals("blah"))
sourceALE[g].setForeground(blah);
}
}
}
scan.close();
}
scanner.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(q1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("OUT");
}
}
1) It is good design to separate your data from the presentation. Are you doing this already? What I mean is you should be looking to store answers in an object outside of the jPanel class. (As 'Hovercraft Full Of Eels' suggested)
2) Consider making the data objects Serializable objects. If you're not planning on storing the data across a network or anything complicated/peculiar this should work out for you as it will facilitate save/load operations
3) Once you have your data separate from the GUI a check for changes becomes very easy. Assume that on any operation that takes you away from the page it "saves" its state. If the current state is different from the saved state then prompt the user to save (if it doesn't by default)