I am making an examination system desktop application in Java Swing.
I have a frame named 'show_class_question' frame.
This frame is used to display questions and four options from database.
Whenever user clicks 'Next' button I am calling this same frame (show_class_question) again and agian from itself (self - refrencing) upto total number of questions (say 20).
show_class_question --(user clicks next button on it)--> show_class_question

Now I would like to add a countdown timer, whose value will be retrieved from database. I have stored minutes in database in an int column.
And whenever user clicks next button the remaining time is passed to the same frame (show_class question) along with other values.
This process continues unless user:
* answers all question
* or clicks on 'End exam' button
* or when the countdown becomes zero
When any of the above happens I want to call 'user_Result' frame

My problems are I am not able to pass remaining time in correct way whenever user clicks 'next' button.
Although I somehow managed to manipulate the countdown code I got from internet and passed the remaining time on next frame, but the 'user_Result' frame is being called several times. Suppose I clicked 'Next' button on 'show_class_question' 7 times, the 'user_Result' frame is getting called 7 times and even before the countdown is zero.

I am posting my code below, I request you to correct it or be genorous enough and provide me with a better and correct way of doing it.
-Thank you

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package question_package;

import db_con_details.Connection_const;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import result_package.User_result;
import user_package.School_stream_selection;
import user_package.course_selection;

/**
 *
 * @author dan
 */
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 for op1 2 3 4 and 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 at top  = "+flag);
        System.out.println("score value at top  = "+score);
        System.out.println("no_of_q_a value at top  = "+no_of_quest_answered);
        System.out.println("q_id value at top  = "+q_id);

        initComponents(); 
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        class_name_lb = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jRadioButton5 = new javax.swing.JRadioButton();
        jLabel9 = new javax.swing.JLabel();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        headernmlb = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        queslb = new javax.swing.JLabel();
        jRadioButton3 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jRadioButton4 = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(1366, 728));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(1366, 806));

        jButton2.setBackground(new java.awt.Color(126, 196, 221));
        jButton2.setText("End Exam");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Course: ");

        class_name_lb.setText("jLabel8");

        jLabel2.setText("Dear ");

        jRadioButton5.setBackground(java.awt.Color.white);
        buttonGroup1.add(jRadioButton5);
        jRadioButton5.setText("jRadioButton2");

        jLabel9.setText("-");

        jRadioButton2.setBackground(java.awt.Color.white);
        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("jRadioButton2");

        jButton3.setBackground(new java.awt.Color(126, 196, 221));
        jButton3.setText("Back");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        headernmlb.setText("jLabel3");

        jLabel8.setText(":");

        jLabel5.setText("Question");

        jLabel7.setText("jLabel7");

        queslb.setText("jLabel8");

        jRadioButton3.setBackground(java.awt.Color.white);
        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("jRadioButton2");

        jButton1.setBackground(new java.awt.Color(126, 196, 221));
        jButton1.setText("Next");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Answer the following questions");

        jLabel10.setText("jLabel10");

        jRadioButton4.setBackground(java.awt.Color.white);
        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText("jRadioButton2");

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/new4 - Copy.png"))); // NOI18N

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/new2 - Copy.png"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
        jLabel1.setText("EXAMINATION SYSTEM");
        jLabel1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jLabel1FocusGained(evt);
            }
        });

        jLabel12.setText("Time Remaining:");

        jLabel13.setText("jLabel13");

        jLabel14.setText("minutes");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(108, 108, 108)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jRadioButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                                                .addComponent(jRadioButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jRadioButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 568, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 1, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 561, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(7, 7, 7))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jButton3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel12)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel13)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel14))
                                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 561, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGap(2, 2, 2)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(headernmlb)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(class_name_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                                    .addComponent(jLabel3)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(11, 11, 11)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(queslb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGap(34, 34, 34)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 624, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14))
                .addGap(13, 13, 13)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(headernmlb)
                    .addComponent(jLabel4)
                    .addComponent(class_name_lb)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addGap(53, 53, 53)
                .addComponent(jLabel3)
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(queslb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGap(48, 48, 48)
                .addComponent(jRadioButton2)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton3)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton4)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton5)
                .addGap(33, 33, 33)
                .addComponent(jButton1)
                .addGap(35, 35, 35)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(130, 130, 130))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your 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 and 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 at 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 your handling code here:

        // user ended exam in between

        int response = JOptionPane.showConfirmDialog(rootPane, "You will not be able to answer any 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 your 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 FROM exam_pattern_in_use WHERE pid = '1' ";
             ResultSet rs6 = st.executeQuery(sql6);
             String pattern_id = null;
             while(rs6.next())
             {
                  pattern_id = rs6.getString("pattern_id");
             }
               String sq5 = "SELECT * FROM exam_pattern WHERE 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 FROM school_stream_level WHERE course_name = '"+class_name+"' and stream_name = '"+stream_name+"' ";
              // s_id in class_question_table = c_id in course_level
              // dono 1 hi cheez represent kar rahe he
              // dono me FK relationship he


               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 * FROM class_ques_table WHERE s_id = '"+s_id+"' and  difficulty_level = '"+difficulty_level+"' ORDER BY RAND() LIMIT 1";

             }
             else
             {
                      sql2 = "SELECT * FROM class_ques_table WHERE s_id = '"+s_id+"' and  difficulty_level = '"+difficulty_level+"' and q_id NOT IN(SELECT q_id FROM asked_ques_table) ORDER BY 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 INTO asked_ques_table VALUES('0', '"+q_id+"')";
                      st.executeUpdate(sql00);



        }
        catch(Exception e){

        }
    }                                 

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your 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 your handling code here:
    }                                   

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Show_class_question.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Show_class_question.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Show_class_question.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Show_class_question.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Show_class_question().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel class_name_lb;
    private javax.swing.JLabel headernmlb;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JLabel queslb;
    // End of variables declaration                   
}

Recommended Answers

All 7 Replies

well, I'm not going through all of that NetBeans generated Swing "code", but what I think you are looking for is not showing a new JFrame, rather hiding the current JPanel that is shown, and show another one, that is placed on the exact same coordinates, but was hidden as long as the other one was visible.

public void switchJPanels(){
  jPanel1.setVisible(!jPanel1.isVisible());
  jPanel2.setVisible(!jPanel1.isVisible());
}

@stultuske agree with, use CardLayout instead of replacing JPanels programatically

I'm not reading all that code either, but it sounds like you need a single controller class that deals with the database, keeps the timer(s) and creates or re-displays the question frame(s) as required.

... and learn how to write code, instead of generating it. generated code (well, especially NetBeans generated code) is a big pain in the a-- if you try to maintain it later, without creating regression-bugs.

well how do I use cardlayout I have no idea (well I know u will say learn, but I have to submit this app by tomorrow ) , cant you guys please do that for me please

cant you guys please do that for me please

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

"do that for me", as in help you cheat on your tasks? think not. if you have to submit this tomorrow, that tells me that:
a. it has been covered in class, meaning you had the time to study
b. you knew this before today. trying to get all the work done at the last moment is very bad.

take a look at this to learn how to use CardLayout.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.