java - How to call another frame when countdown ends in SWING -


i making examination system desktop application in java swing. have frame named 'show_class_question' frame. frame used display questions , 4 options database. whenever user clicks 'next' button calling same frame (show_class_question) again , again (self - referencing) upto total number of questions (say 20).

show_class_question --(user clicks next button on it)--> show_class_question

now add countdown timer, value retrieved database. have stored minutes in database in int column. , whenever user clicks next button remaining time passed same frame (show_class question) along other values.

this process continues unless user:

  1. answers question
  2. or clicks on 'end exam' button
  3. or when countdown becomes zero

when of above happens want call 'user_result' frame

my problems not able pass remaining time in correct way whenever user clicks 'next' button. although somehow managed manipulate countdown code got internet , passed remaining time on next frame, 'user_result' frame being called several times. suppose clicked 'next' button on 'show_class_question' 7 times, 'user_result' frame getting called 7 times , before countdown zero.

i posting code below, request correct or genorous enough , provide me better , correct way of doing it.

-thank you

public class show_class_question extends javax.swing.jframe {  private timer timer; string header_name; string class_name; string stream_name; string u_id; string mix_course_header; int d=0;  public void end_exam() {  truncate_table();         user_result ur = new user_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);         ur.setvisible(true);         this.dispose(); } public void truncate_table()  {      try {         class.forname("com.mysql.jdbc.driver");          connection con = drivermanager.getconnection(connection_const.db_url,connection_const.user_name,connection_const.password);          statement st = con.createstatement();           string sqla = "truncate table asked_ques_table";          st.executeupdate(sqla);      } catch (exception e) {     }  }   /*  * declaring publics strings op1 2 3 4 , answer  */ string ques = null; string q_id = null; string op1 = null; string op2 = null; string op3 = null; string op4 = null; string answer = null;  // declaring logical variables int flag; float score; int no_of_quest_answered; int no_of_correct_answers;  //exam pattern variables string difficulty_level;  int total_no_of_ques;  int time_limit;  int points_awarded;  int negative_marking; // 0 --> no, 1 --> yes  float points_deducted;    // countdown variables  long remaining; long passed; long limit;  /**  * creates new form show_class_question  */ public show_class_question() {     initcomponents(); }  // our new constructor  public show_class_question(string h_name, string pclass_name, string pstream_name, int pflag, string pu_id) {     header_name = h_name;     class_name = pclass_name;     stream_name = pstream_name;     u_id = pu_id;     flag = pflag;     initcomponents(); }   //self refrencing constructor public show_class_question(int pflag, float pscore, int p_no_of_ques_ans, string pq_id, string pclass_name, string pstream_name, string namelb,  string pu_id, int pno_of_correct_ans,  long plimit,long ppassed) {     flag = pflag;     score = pscore;     no_of_quest_answered = p_no_of_ques_ans;     q_id = pq_id;     class_name = pclass_name;     stream_name = pstream_name;     header_name = namelb;     u_id = pu_id;     no_of_correct_answers = pno_of_correct_ans;     //remaining = premaining;    passed = ppassed;     limit = plimit;     system.out.println("rem inside cons= "+remaining);       //x2 = px;      system.out.println("d cls name= "+class_name);     system.out.println("d strm name= "+stream_name);      system.out.println("flag value @ top  = "+flag);     system.out.println("score value @ top  = "+score);     system.out.println("no_of_q_a value @ top  = "+no_of_quest_answered);     system.out.println("q_id value @ top  = "+q_id);      initcomponents();  }   private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                              // todo add handling code here:   limit=remaining;      string user_answer;     if (flag == 0) {          score = 0;         no_of_quest_answered = 0;     }      if (jradiobutton2.isselected()) {         user_answer = jradiobutton2.gettext();     } else if (jradiobutton3.isselected()) {         user_answer = jradiobutton3.gettext();     } else if (jradiobutton4.isselected()) {         user_answer = jradiobutton4.gettext();     } else if (jradiobutton5.isselected()) {         user_answer = jradiobutton5.gettext();     } else {         user_answer = null;     }       //checking answer , answered or not      if (user_answer != null) {          no_of_quest_answered++;        if(negative_marking==0)         {             if(user_answer.equals(answer))             {                 score=score+(1*points_awarded);                 no_of_correct_answers++;              }         }         else if(negative_marking==1)         {              if(user_answer.equals(answer))             {                  score=score+(1*points_awarded);                  no_of_correct_answers++;              }              else              {                  score= (score-points_deducted);               }         }      }      flag++;     system.out.println("flag value @ bottom  = " + flag);      if (flag < total_no_of_ques) //starting frm 0     {          show_class_question s_c_q = new show_class_question(flag, score, no_of_quest_answered, q_id, class_name, stream_name,  header_name, u_id,no_of_correct_answers,  limit, passed);         s_c_q.setvisible(true);         this.dispose();     } else {         truncate_table();         user_result ur = new user_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);         ur.setvisible(true);         this.dispose();     } }                                          private void jbutton2actionperformed(java.awt.event.actionevent evt) {                                              // todo add handling code here:      // user ended exam in between      int response = joptionpane.showconfirmdialog(rootpane, "you not able answer more questions. continue?");     if(response==0)     {      truncate_table();     user_result ur = new user_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking,points_awarded,points_deducted,difficulty_level,no_of_correct_answers,total_no_of_ques);     ur.setvisible(true);     this.dispose();     } }                                          private void formwindowopened(java.awt.event.windowevent evt) {                                       // todo add handling code here:      try{          class.forname("com.mysql.jdbc.driver");         connection con = drivermanager.getconnection(connection_const.db_url,connection_const.user_name, connection_const.password);         statement st = con.createstatement();          string sql6  = "select pattern_id exam_pattern_in_use pid = '1' ";          resultset rs6 = st.executequery(sql6);          string pattern_id = null;          while(rs6.next())          {               pattern_id = rs6.getstring("pattern_id");          }            string sq5 = "select * exam_pattern pattern_id='"+pattern_id+"' ";          resultset rs5 = st.executequery(sq5);          string db_diff_lev = null;          string db_time_limit = null;          string db_tot_ques = null;          string db_points_award = null;          string db_neg_marking = null;          string db_points_ded = null;           while(rs5.next())          {              db_diff_lev = rs5.getstring("difficulty_level");              db_tot_ques = rs5.getstring("tot_no_of_ques");              db_time_limit = rs5.getstring("time_limit");              db_points_award = rs5.getstring("points_awarded_for_correct_ans");              db_neg_marking = rs5.getstring("negative_marking");              db_points_ded = rs5.getstring("points_deduced_for_wrong_ans");          }          int my_time_limit = integer.parseint(db_time_limit);          system.out.println("my time lim = "+my_time_limit);       ////------------------------->timer code start here     final long current = system.currenttimemillis();      try {         if(flag==0)             limit = my_time_limit*1000; // x seconds               timer = new timer(1000, new actionlistener() {              public void actionperformed(actionevent event) {              long time = system.currenttimemillis();              passed = time - current;              remaining = limit - passed;              system.out.println("rem time ="+remaining);            if(remaining >= 1) {               long seconds = remaining/1000;             long minutes = seconds/60;             long hours = minutes/60;             jlabel13.settext(string.format("%02d:%02d:%02d", hours, minutes, seconds%60));             } else {             timer.stop();            timer.setrepeats(false);            truncate_table();            setvisible(false);            dispose();         user_result ur = new user_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);         ur.setvisible(true);            d=1;         }         }          });        timer.start();      } catch(numberformatexception nfe) {       // debug/report here        nfe.printstacktrace();     }      ////------------------------->timer code end here       settitle("question: "+(flag+1));     headernmlb.settext(header_name);     class_name_lb.settext(class_name);     jlabel10.settext(stream_name);     mix_course_header = class_name+"-"+stream_name;         if(flag==0)     {         jbutton3.setvisible(true);     }     else     {         jbutton3.setvisible(false);     }            /*  if(d==1)                   {                         truncate_table();                         user_result ur = new user_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);                         ur.setvisible(true);                         this.dispose();                   }     */           difficulty_level = db_diff_lev;          total_no_of_ques = integer.parseint(db_tot_ques);          points_awarded = integer.parseint(db_points_award);          negative_marking =  integer.parseint(db_neg_marking);          points_deducted =  float.parsefloat(db_points_ded);          system.out.println("fractional points ded = "+points_deducted);             string sql = "select s_id school_stream_level course_name = '"+class_name+"' , stream_name = '"+stream_name+"' ";           // s_id in class_question_table = c_id in course_level           // dono 1 hi cheez represent kar rahe           // dono me fk relationship              resultset rs = st.executequery(sql);             string s_id=null;           while(rs.next())          {               s_id = rs.getstring("s_id");          }            string sql2;           if(flag==0)          {              sql2 = "select * class_ques_table s_id = '"+s_id+"' ,  difficulty_level = '"+difficulty_level+"' order rand() limit 1";           }          else          {                   sql2 = "select * class_ques_table s_id = '"+s_id+"' ,  difficulty_level = '"+difficulty_level+"' , q_id not in(select q_id asked_ques_table) order rand() limit 1";          }             resultset rs2 =st.executequery(sql2);           system.out.println("under rs2");                while(rs2.next())               {                    ques = rs2.getstring("question");                    q_id = rs2.getstring("q_id");                    op1 = rs2.getstring("op1");                    op2 = rs2.getstring("op2");                    op3 = rs2.getstring("op3");                    op4 = rs2.getstring("op4");                    answer = rs2.getstring("answer");                    system.out.println("q_id db = "+q_id);               }                    jlabel7.settext(""+(flag+1));                   queslb.settext(ques);                   jradiobutton2.settext(op1);                   jradiobutton3.settext(op2);                   jradiobutton4.settext(op3);                   jradiobutton5.settext(op4);                     string sql00 = "insert asked_ques_table values('0', '"+q_id+"')";                   st.executeupdate(sql00);        }     catch(exception e){      } }                                   private void jbutton3actionperformed(java.awt.event.actionevent evt) {                                              // todo add handling code here:     if (flag == 0) {         //   jbutton3.setvisible(true);         school_stream_selection s_s_s = new school_stream_selection(header_name, class_name, u_id);         s_s_s.setvisible(true);         dispose();      }  }                                          private void jlabel1focusgained(java.awt.event.focusevent evt) {                                         // todo add handling code here: }                                     ...and few more lines... 


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -