Solved

How to create a reset: restart with blank panels

Posted on 2004-08-07
9
249 Views
Last Modified: 2013-12-03
How could I create a method that would reset the panels after they have been filled with questions and answers by using a reset button?


package trainer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.text.MessageFormat;
/**
 *
 * @author  Geert & Isabel
 */
public class TrainingOptions extends javax.swing.JPanel {
   
    static private int MAX_NR_OF_CORRECT_ANSWERS_PER_Q = 3;
    static private int MAX_NR_OF_QUESTIONS = 15;
   
    Question myQuestions[];
    TrainerMain theMain = null;
    List theDMsToPickFrom = new ArrayList();
    List theQsToPickFrom = new ArrayList();
    boolean beginAtTheHead = true;
   
    private int currentNrOfQuestions;
   
    /** Creates new form TrainingOptions */
    public TrainingOptions(TrainerMain main) {
        currentNrOfQuestions = 0;
        theMain = main;
        initComponents();
        initQuestions();
    }
   
    private void initQuestions() {
       // 1. Create the array
       myQuestions = new Question[26];
       
       // 2. Fill it
       
       // 2.a. questions with 1 parameter
        List paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        myQuestions[0] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> in them?", 1, paraTypes );

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[1] = new Question(
        "Which defensive move or moves use a handstrike to the <FONT color=red>{0}</b></FONT> in them? ", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        myQuestions[2] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> in them?", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[3] = new Question(
        "Which defensive move or moves have a kick to the <FONT color=red><b>{0}</FONT> in them?", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        myQuestions[4] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> in them?", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        myQuestions[5] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> in them?", 1, paraTypes);

        // 2.b. questions with 2 parameters

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.STRIKE);
        myQuestions[6] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.KICK);
        myQuestions[7] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.STRIKE);
        myQuestions[8] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.KICK);
        myQuestions[9] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.KICK);
        myQuestions[10] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.STRIKE);
        myQuestions[11] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[12] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> to the <FONT color=red><b>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[13] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> to the <FONT color=red><b>{1}</b></FONT> in them?", 2, paraTypes);

        // 2.c. questions with 3 parameters

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[14] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[15] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[16] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[17] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[18] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a<FONT color=blue>{1}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[19] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> in them?", 3, paraTypes);

        // 2.d. questions with 4 parameters

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[20] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> to the <FONT color=red><b>{1}</b></FONT> and a <FONT color=blue>{2}</b></FONT> to the <FONT color=red><b>{3}</b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[21] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> to the <FONT color=red><b>{1}</b></FONT> and a <FONT color=blue><b>{2}</b></FONT> to the <FONT color=red><b>{3}</b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[22] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> to the <FONT color=red><b>{2}</b></FONT> and a <FONT color=blue><b>{1}</b></FONT> to the <FONT color=red><b>{3}</b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[23] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></b></FONT> to the <FONT color=red><b>{2}</b></b></FONT> and a <FONT color=blue><b>{1}</b></FONT> to the <FONT color=red><b>{3}</b></b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[24] = new Question(
        "Which defensive move or moves have a <FONT color=blue><b>{0}</b></b></FONT> and a <FONT color=blue><b>{1}</b></b></FONT> a <FONT color=blue><b>{2}</b></b></FONT> to the <FONT color=red><b>{3}</b></b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[25] = new Question(
        "Which defensive move or moves have a <FONT color=blue><b>{0}</b></b></FONT> and a <FONT color=blue><b>{1}</b></b></FONT> and a <FONT color=blue><b>{2}</b></b></FONT> to the <FONT color=red><b><b>{3}</b></b></FONT> in them?", 4, paraTypes);

    }
   
    private String composeMyText(String format, Object[] arguments) {
         return MessageFormat.format(format, arguments);
    }

    public String composeMyText(String format, Object para1) {  // use this for one parameter
         return composeMyText(format, new Object[] { para1});
    }

    public String composeMyText(String format, Object para1, Object para2) {  // use this for 2 parameters
         return composeMyText(format, new Object[] { para1, para2 });
    }

    public String composeMyText(String format, Object para1, Object para2, Object para3) { // use this for 3 parameters
         return composeMyText(format, new Object[] { para1, para2 , para3});
    }

    public String composeMyText(String format, Object para1, Object para2, Object para3, Object para4) { // use this for 4 parameters
         return composeMyText(format, new Object[] { para1, para2, para3, para4});
    }
   
    /** 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.
     */
    private void initComponents() {
        mainPanel = new javax.swing.JPanel();
        mainPanel.setBackground(new Color(250, 250, 250));
        theScrollPane = new javax.swing.JScrollPane();
        theScrollPane.setBackground(new Color(250, 250, 250));
        insideScrollPanel = new javax.swing.JPanel();
        insideScrollPanel.setBackground(new Color(250, 250, 250));
        questionsPanel = new javax.swing.JPanel();
        questionsPanel.setBackground(new Color(250, 250, 250));
        southPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        startButton = new javax.swing.JButton();
        btnMgrButton = new javax.swing.JButton("Button Manager");

        setLayout(new java.awt.BorderLayout());

        mainPanel.setLayout(new java.awt.BorderLayout());
        mainPanel.setBackground(new Color(250, 250, 250));
        insideScrollPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
       
        questionsPanel.setLayout(new java.awt.GridBagLayout());
       
        insideScrollPanel.add(questionsPanel);

        theScrollPane.setViewportView(insideScrollPanel);

        mainPanel.add(theScrollPane, java.awt.BorderLayout.CENTER);

        add(mainPanel, java.awt.BorderLayout.CENTER);

        startButton.setText("DM's Test");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(startButton);

        btnMgrButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMgrButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(btnMgrButton);
       
        southPanel.add(buttonPanel);

        add(southPanel, java.awt.BorderLayout.SOUTH);

    }
   
    private void btnMgrButtonActionPerformed(java.awt.event.ActionEvent evt) {
       
        ButtonMgrPnl pnl = new ButtonMgrPnl(theMain.getButtonMap());
        theMain.showModalDialog(pnl, "Button Manager");
    }

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
        DM theDMs[] = theMain.getDMs(); // Get the DMs array we built up in TrainerMain
        theDMsToPickFrom = new ArrayList();
        for (int i=0; i<theDMs.length; i++)
            theDMsToPickFrom.add( theDMs[i] );
        theQsToPickFrom = new ArrayList();
        for (int i=0; i<myQuestions.length; i++)
            theQsToPickFrom.add( myQuestions[i] );
        System.out.println("Number of available DMs = " + theDMsToPickFrom.size());  // <<<<<<<<<<<<<<<<

        for (int i=1; i<=MAX_NR_OF_QUESTIONS; i++)
            pickAndPresentOneQuestion();
        questionsPanel.revalidate();
        questionsPanel.repaint();
    }
   
    private DM pickAndPresentOneQuestion() {
        Random random = new Random();
        // 1. Randomly pick out a question
        Question myQ = (Question) theQsToPickFrom.get( random.nextInt(theQsToPickFrom.size()) );
        theQsToPickFrom.remove(myQ);
        if (myQ==null) return null; // should never happen
       
//        System.out.println(myQ.getFormat());
       
        int nrOfParameters = myQ.getNumberOfParameters();
       
        // 2. Randomly pick a DM
        boolean theDMisOK = false;
        DM theDM = null;
        while ( !theDMisOK ) {
           theDM = (DM) theDMsToPickFrom.get( random.nextInt(theDMsToPickFrom.size()) );
           List paraTypes = myQ.getParameterTypes();
           Iterator it = paraTypes.iterator();
           int nrOfParaTypesOK = 0;
           Integer prevType = Question.STANCE;
           while (it.hasNext()) {
               Integer type = (Integer)it.next();
               if ( type.equals(Question.STRIKE) || type.equals(Question.STRIKE_LOCATION) ) {
                   if ( prevType.equals(Question.STRIKE) || prevType.equals(Question.STRIKE_LOCATION) ) {
                       if ( theDM.getStrike(1)!=null ) // DM needs to have two strike(loc)s
                           nrOfParaTypesOK++;
                   }
                   else if ( theDM.getStrike(0)!=null )
                       nrOfParaTypesOK++;
               }
               else if ( type.equals(Question.KICK) || type.equals(Question.KICK_LOCATION) ) {
                   if ( prevType.equals(Question.KICK) || prevType.equals(Question.KICK_LOCATION) ) {
                       if ( theDM.getKick(1)!=null ) // DM needs to have two kick(loc)s
                           nrOfParaTypesOK++;
                   }
                   else if ( theDM.getKick(0)!=null )
                       nrOfParaTypesOK++;
               }
               else if ( type.equals(Question.BLOCK) ) {
                   if ( theDM.getBlock(0)!=null )
                       nrOfParaTypesOK++;
               }
               else if ( type.equals(Question.STANCE) ) {
                   if ( theDM.getStance()!=null )
                       nrOfParaTypesOK++;
               }
           }
           theDMisOK = ( nrOfParameters==nrOfParaTypesOK );
        }
        System.out.println("The picked DM is " + theDM.getName());
//        theDMsToPickFrom.remove(theDM);
       
        // 3. Randomly pick a good answer (if multiple available in the picked DM)
        // and
        // 4. Compose the question with them
        List paraTypes = myQ.getParameterTypes();
        Iterator it = paraTypes.iterator();
        int nrOfParaTypesOK = 0;
        List correctAnswers = new ArrayList();
        Integer prevType = Question.STANCE;
        List kickLocationIndex = new ArrayList();
        List strikeLocationIndex = new ArrayList();
        while (it.hasNext()) {
           Integer type = (Integer)it.next();
           if ( type.equals(Question.STRIKE) ) {
              int i = 0;                                                                             // <<<<<<<<<<
              do                                                                                      // <<<<<<<<<<
                i = random.nextInt(theDM.getNrOfStrikes());                        // <<<<<<<<<<
              while (correctAnswers.contains(theDM.getStrike(i)));              // <<<<<<<<<<
              correctAnswers.add( theDM.getStrike(i) );
              strikeLocationIndex.add(0, new Integer(i)); // add a the head
           }

           else if ( type.equals(Question.STRIKE_LOCATION) ) {
              if (strikeLocationIndex.size()==0) {
                  int i = random.nextInt(theDM.getNrOfStrikes());
                  correctAnswers.add( theDM.getStrikeLocation(i) );
              }
              else {
                  Integer i = (Integer)strikeLocationIndex.get(strikeLocationIndex.size()-1);
                  correctAnswers.add( theDM.getStrikeLocation(i.intValue()) );
                  strikeLocationIndex.remove(strikeLocationIndex.size()-1); // remove at the tail
              }
           }
           else if ( type.equals(Question.KICK) ) {
              int i = 0;                                                                  // <<<<<<<<<<<
              do                                                                           // <<<<<<<<<<<
                  i = random.nextInt(theDM.getNrOfKicks());             // <<<<<<<<<<<
              while (correctAnswers.contains(theDM.getKick(i)));     // <<<<<<<<<<<
              correctAnswers.add( theDM.getKick(i) );
              kickLocationIndex.add(0, new Integer(i)); // add a the head
           }

           else if ( type.equals(Question.KICK_LOCATION) ) {
              if (kickLocationIndex.size()==0) {
                  int i = random.nextInt(theDM.getNrOfKicks());
                  correctAnswers.add( theDM.getKickLocation(i) );
              }
              else {
                  Integer i = (Integer)kickLocationIndex.get(kickLocationIndex.size()-1);
                  correctAnswers.add( theDM.getKickLocation(i.intValue()) );
                  kickLocationIndex.remove(kickLocationIndex.size()-1); // remove at the tail
              }
           }
           else if ( type.equals(Question.BLOCK) ) {
              correctAnswers.add(
                     theDM.getBlock( random.nextInt(theDM.getNrOfBlocks()) ) );
           }
           else if ( type.equals(Question.STANCE) ) {
               correctAnswers.add( theDM.getStance() );
           }
        }
       
        String theQuestion = composeMyText(myQ.getFormat(), correctAnswers.toArray());
//        System.out.println(theQuestion);
       
        // Search for other good answers
        DM allDMs[] = theMain.getDMs(); // Get the DMs array we built up in TrainerMain
        DM thePossibleAnswers[] = new DM[5];
        thePossibleAnswers[0] = theDM; // put in the first good answer
        int nrOfGoodAnswers = 1;
       
        for ( int i=0;
              i<allDMs.length && nrOfGoodAnswers < MAX_NR_OF_CORRECT_ANSWERS_PER_Q;
              i++) {
           // Make sure we don't deal with an answer we already have
           boolean skipThisOne = false;
           for (int j=0; j<nrOfGoodAnswers; j++) {
               if ( allDMs[i].getName().equals(thePossibleAnswers[j].getName()) ) {
                   skipThisOne = true;
                   break;
               }
           }
           if (skipThisOne)
               continue; // with another one
           
           // allDMs.get(i) is the DM to test.
           DM theDMToTest = allDMs[i];
           // thePossibleAnswers[0] is the good one
           it = paraTypes.iterator();
           nrOfParaTypesOK = 0;
           boolean found = false;
           while (it.hasNext()) {
               Integer type = (Integer)it.next();
               if ( type.equals(Question.STRIKE) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfStrikes(); k++)
                       if ( theDMToTest.getStrike(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               if ( type.equals(Question.STRIKE_LOCATION) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfStrikes(); k++)
                       if ( theDMToTest.getStrikeLocation(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               if ( type.equals(Question.KICK) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfKicks(); k++)
                       if ( theDMToTest.getKick(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               if ( type.equals(Question.KICK_LOCATION) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfKicks(); k++)
                       if ( theDMToTest.getKickLocation(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               else if ( type.equals(Question.BLOCK) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfBlocks(); k++)
                       if ( theDMToTest.getBlock(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               else if ( type.equals(Question.STANCE) ) {
                   if ( theDMToTest.getStance()!=null &&
                        theDMToTest.getStance().equals(
                          (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                       nrOfParaTypesOK++;
                   }
                   else
                      break;
               }
           }
           if ( nrOfParameters==nrOfParaTypesOK ) {
               // add the found DM to the possible answers:
               thePossibleAnswers[nrOfGoodAnswers] = theDMToTest;
               nrOfGoodAnswers++;
           }
        } // for-loop over theDMs array
       
        // Now we have nrOfGoodAnswers good answers.
        // Let's fill up the rest of the array with wrong answers
        int nrOfAnswers = nrOfGoodAnswers;
        while (nrOfAnswers!=5) {
            DM anotherDM = allDMs[ random.nextInt(allDMs.length) ];
           
            // Make sure we don't deal with an answer we already have
            boolean skipThisOne = false;
            for (int j=0; j<nrOfAnswers; j++) {
               if ( anotherDM.getName().equals(thePossibleAnswers[j].getName()) ) {
                   skipThisOne = true;
                   break;
               }
            }
            if (skipThisOne)
               continue; // with another one
            else {
               // add the found DM to the possible answers:
               thePossibleAnswers[nrOfAnswers] = anotherDM;
               nrOfAnswers++;
            }
        }
       
        // shuffle the answers
        List lstAnswers = Arrays.asList(thePossibleAnswers);
        Collections.shuffle(lstAnswers);
       
        // Present the question in an added panel
        QPanel qPanel = new QPanel();
        qPanel.setBackground(new Color(250, 250, 250));
        qPanel.setQuestion(theQuestion, currentNrOfQuestions+1);
        for (int i=0; i<5; i++)
            qPanel.setAnswer(thePossibleAnswers[i].getName(), i+1);
       
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = currentNrOfQuestions;
        currentNrOfQuestions++;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        questionsPanel.add(qPanel, gridBagConstraints);
       
        return theDM;
    }
   
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel insideScrollPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel questionsPanel;
    private javax.swing.JPanel southPanel;
    private javax.swing.JButton startButton;
    private javax.swing.JScrollPane theScrollPane;
    private javax.swing.JButton btnMgrButton;
}
0
Comment
Question by:Drop_of_Rain
  • 5
  • 4
9 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 200 total points
ID: 11744182
Quite easy:

JButton resetButton = new JButton("Reset');
resetButton.addActionListener( new ActionListener() {

   public void actionPerformed(ActionEvent evt) {
       questionsPanel.removeAll();
   }
});

// Add that button where you want
someContainer.add(resetButton);
0
 

Author Comment

by:Drop_of_Rain
ID: 11744193
About checking the answers to the questions. I want to post this question, I will need to post the QPanel and TrainingOptions panel?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11744204
Yeah, that way nobody can complain ;°)
0
 

Author Comment

by:Drop_of_Rain
ID: 11744222
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 37

Expert Comment

by:zzynx
ID: 11744308
Forgot somehting:

        resetButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                questionsPanel.removeAll();
                questionsPanel.revalidate();     // <<<<<<<<<<<<<  You have to add these two lines or the panel won't be immediately repainted
                questionsPanel.repaint();        // <<<<<<<<<<<<<  (and so it would look as if it wasn't cleared)
            }
        });
0
 

Author Comment

by:Drop_of_Rain
ID: 11744331
The same thing is happening. The numbers start again at 16 thq 31 the 46 then it freezes.
0
 

Author Comment

by:Drop_of_Rain
ID: 11744336
It freezes now on the second try.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11744630
It shouldn't no more with the code just posted in the other Q.
Is that OK?
0
 

Author Comment

by:Drop_of_Rain
ID: 11744647
Yes, I see what you mean about forcing the flow of the buttons
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now