java - Method does not work properly before executing System.exit(0) -
i new in java, have swing application menu file/exit, application editor, when select option exit menu application ask me if want save changes before exit, see exit code below
class actionexit extends abstractaction { public actionexit() { super("exit",icons.iconexit); setenabled(true); putvalue("menu", "file"); putvalue("menuindex", "99"); } public void actionperformed(actionevent evt) { saveandclose(); <----does not execute instructions savesettings(); system.exit(0); } }
the problem have in method saveandclose(), here check if document has changed , display dialog question/options (save? yes,no), execute other methods connect database , apply changes made document etc. method executes first instructions , seems being interrupted (i put lot of "system.out.println" find method stops), application returns , continues next method , ends. if debug application works perfectly, not work when runs normally. why happens?, can make/force application save changes before exit?
** here code of saveandclose **
basically doing here is, main frame try find component(panel) want close, once find execute instruction:
(panelmember)component[y]).closetab()
i reuse method, called in option in menu (window/close-panel) , when click in panel contains document , select 'x' close it
public void saveandclose() { component[] component =this.getcomponents(); (int y = 0; y < component.length; y++) { if ((component[y] instanceof container)) { saveandclose2((container)component[y]); } } } public void saveandclose2(container pcont) { component[] component = pcont.getcomponents(); (int y = 0; y < component.length; y++) { if ((component[y] instanceof container)) { if ((component[y] instanceof panelmember)) { ((panelmember)component[y]).closetab(); } closetabsinpanel2((container)component[y]); } } }
here rest of programs/methods executed, thank help
panlemember.java
public void closetab() { environment.members.close(projectmember, false); }
members.java
public class members { public void close(projectmember projectmember, boolean cache) { if (environment.toolmanager == null) { return; } if (!projectmember.getmember().isoktoclose()) { return; } environment.toolmanager.close(projectmember, cache); }
member.java
public boolean isoktoclose() { listenermember temp[] = (listenermember[])listlisteners.toarray(new listenermember[listlisteners.size()]); for(int x = 0; x < temp.length; x++) { if(!temp[x].isoktoclose(this)) return false; } return true; }
panelmember.java - here when display dialog ask user save changes or not
public boolean isoktoclose(member member) { if (currentlysaving) { return false; } if (sourceparser.isdirty()) { int option = joptionpane.showconfirmdialog(keyboardfocusmanager.getcurrentkeyboardfocusmanager().getactivewindow(), projectmember.getmember().getname() + " has been changed. save changes?", "save changes", 1, 3); if (option == 2) { return false; } if (option == 0) { closeaftersave = true; startsave(); return false; } } return true; } public void startsave() { final string procedurelibrary; try { procedurelibrary = getprocedurelibrary(); } catch (sqlexception e) { joptionpane.showmessagedialog(keyboardfocusmanager.getcurrentkeyboardfocusmanager().getactivewindow(), e.getmessage()); e.printstacktrace(); return; } if (procedurelibrary == null) { return; } system.out.println("entering startsave method"); //gs01 selectedtabbeforesave = jtabbedpane1.getselectedindex(); thread t = new thread() { // private final string val$procedurelibrary; public void run() { setpriority(1); try { save(procedurelibrary); } catch (exception e) { joptionpane.showmessagedialog(keyboardfocusmanager.getcurrentkeyboardfocusmanager().getactivewindow(), e.getmessage()); e.printstacktrace(); } } }; t.start(); } protected void save(string procedurelibrary) throws sqlexception { if (currentlysaving) { return; } currentlysaving = true; actionmembersave.setenabled(false); swingutilities.invokelater(new runnable() { public void run() { panelloading.settext("saving source."); jtabbedpane1.inserttab("status", null, panelstatus, "status", 0); jtabbedpane1.setselectedindex(0); jtabbedpane1.setenabled(false); } }); projectmember.member.save(procedurelibrary, sourceparser, this); currentlysaving = false; if (closeaftersave) environment.members.close(projectmember, false); }
member.java - here put lot of system.out.println see process stops, last message displayed "conection:", "synchronized", rest of them never displayed
public void save(string procedurelibrary, sourceparser parser, listenersave listener) { system.out.println("entering save method"); //gs01 savebulk(parser, listener); } public void savebulk(sourceparser parser, listenersave listener) { connection connection; statement stmt; stringbuffer buffer; string backup; string append; int today, date, row; buffer = new stringbuffer(); backup = null; append = " "; row = 1; system.out.println("entering savebulk method"); //gs01 for(sourceline line = parser.getfirst(); line != null; line = line.getnext()) { if(line.gettext().length() > maxchars + 1) { if(listener != null) { listener.savecomplete(0, false, "line number: " + row + " on " + maxchars + " characters."); } return; } row++; } try { system.out.println("backup file saved :" + savebackup(parser)); //gs01 backup = "backup file saved to: " + savebackup(parser); row = 0; calendar cal = calendar.getinstance(); today = (cal.get(1) - 2000) * 10000; today += (cal.get(2) + 1) * 100; today += cal.get(5); connection = as400system.getconnection(); //sometimes process stops here system.out.println("connection :" + connection); //gs01 synchronized(connection) { system.out.println("synchronized"); //sometimes process stops here as400system.createtempsrctable(maxchars + 12); system.out.println("create temp src table "); //gs01 stmt = connection.createstatement(); system.out.println("conection create statement"); for(sourceline line = parser.getfirst(); line != null; line = line.getnext ()) { buffer.append(line.gettext()); date = line.date; if(line.changed || line.created) { date = today; } line.changed = false; line.created = false; line.date = date; buffer.append(line.date); buffer.append("\n"); row++; if(buffer.length() > 32000) { system.out.println("if buffer.length>32000 " ); //gs01 stmt.execute("call qgpl/prcupload('" + buffer.tostring().replaceall("'", "''") + "', '\n', '" + append + "')"); append = "t"; buffer = new stringbuffer(); if(listener != null) { listener.linesaved(row); } } } system.out.println("if buffer.length>0 " ); //gs01 if(buffer.length() > 0 || append.equalsignorecase(" ")) { system.out.println("call qgpl/prcupload"); //gs01 stmt.execute("call qgpl/prcupload('" + buffer.tostring().replaceall("'", "''") + "', '\n', '" + append + "')"); } stmt.execute(as400system.buildsqlforcmd("cpyf fromfile(qtemp/srcupload) tofile(" + library + "/" + file + ") tombr(" + member + ") mbropt(*replace) fmtopt(*nochk)")); stmt.close(); system.out.println("file copied :" + member); //gs01 } if(listener != null) { system.out.println("savecomplete method"); //gs01 listener.savecomplete(row, true, backup); } system.out.println("parser setdirty"); //gs01 parser.setdirty(false); } catch (exception e) { system.out.println("buffer: (" + buffer.tostring().replaceall("'", "''") + ")"); e.printstacktrace(); if(listener != null) { listener.savecomplete(0, false, e.getmessage() + "\n" + backup); } } system.out.println("nothing"); //gs01 }
Comments
Post a Comment