Wants to get finally selected check boxes. But giving empty or other
This program is a simple exam simulator. I wants to get the check boxes
that I selected finally. But in here I got wrong result.
Constructor and Attributes
private ServerConnector serverConnector;
private QuestionEnrollManagementController questionEnroll;
private QuestionManagementController questionController;
private List<List<Question>> executeList;
private int q;
private int qCount = 1;
private JCheckBox[] checkBoxAr;
private QTemp qTempAction;
private Map<String, QTemp> mpTemp;
public TestPane() {
initComponents();
getDetails();
mpTemp = new HashMap<>();
checkBoxAr = new JCheckBox[7];
for (int i = 0; i < checkBoxAr.length; i++) {
checkBoxAr[i] = new JCheckBox();
}
loadFirstQ();
checkBoxAr[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpA(checkBoxAr[0].getText());
}
});
checkBoxAr[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpB(checkBoxAr[1].getText());
}
});
checkBoxAr[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpC(checkBoxAr[2].getText());
}
});
checkBoxAr[3].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpD(checkBoxAr[3].getText());
}
});
checkBoxAr[4].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpE(checkBoxAr[4].getText());
}
});
checkBoxAr[5].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpF(checkBoxAr[5].getText());
}
});
checkBoxAr[6].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qTempAction.setOpG(checkBoxAr[6].getText());
}
});
}
In the I add the check boxes selected state to QTemp model.
public class QTemp {
private String question;
private String opA;
private String opB;
private String opC;
private String opD;
private String opE;
private String opF;
private String opG;
public QTemp() {
}
public QTemp(String question, String opA, String opB, String opC, String
opD, String opE, String opF, String opG) {
this.question = question;
this.opA = opA;
this.opB = opB;
this.opC = opC;
this.opD = opD;
this.opE = opE;
this.opF = opF;
this.opG = opG;
}
/**
* @return the question
*/
public String getQuestion() {
return question;
}
/**
* @param question the question to set
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* @return the opA
*/
public String getOpA() {
return opA;
}
/**
* @param opA the opA to set
*/
public void setOpA(String opA) {
this.opA = opA;
}
/**
* @return the opB
*/
public String getOpB() {
return opB;
}
/**
* @param opB the opB to set
*/
public void setOpB(String opB) {
this.opB = opB;
}
/**
* @return the opC
*/
public String getOpC() {
return opC;
}
/**
* @param opC the opC to set
*/
public void setOpC(String opC) {
this.opC = opC;
}
/**
* @return the opD
*/
public String getOpD() {
return opD;
}
/**
* @param opD the opD to set
*/
public void setOpD(String opD) {
this.opD = opD;
}
/**
* @return the opE
*/
public String getOpE() {
return opE;
}
/**
* @param opE the opE to set
*/
public void setOpE(String opE) {
this.opE = opE;
}
/**
* @return the opF
*/
public String getOpF() {
return opF;
}
/**
* @param opF the opF to set
*/
public void setOpF(String opF) {
this.opF = opF;
}
/**
* @return the opG
*/
public String getOpG() {
return opG;
}
/**
* @param opG the opG to set
*/
public void setOpG(String opG) {
this.opG = opG;
}
}
I set the options from selected check box states.
In my loadFirstQuestion method, I loaded the first question.
private void loadFirstQ() {
text.setText(executeList.get(0).get(0).getQuestion());
lbQCount.setText(qCount + " of " + executeList.size());
loadAnswers(executeList.get(0).get(0).getqId());
}
This question is getting from the list that I declared executeList. That
list is with the question ids.
In my loadAnswers method I checked the answers from database and visible
check boxes from array.
private void loadAnswers(String qId) {
qTempAction = new QTemp();
jPanel1.removeAll();
try {
List<Question> listQuestion = questionController.performSearch(qId);
for (Question question : listQuestion) {
if (question.getOpA() != null) {
checkBoxAr[0].setText("<html>" +
question.getOpA().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[0]);
}
if (question.getOpB() != null) {
checkBoxAr[1].setText("<html>" +
question.getOpB().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[1]);
}
if (question.getOpC() != null) {
checkBoxAr[2].setText("<html>" +
question.getOpC().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[2]);
}
if (question.getOpD() != null) {
checkBoxAr[3].setText("<html>" +
question.getOpD().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[3]);
}
if (question.getOpE() != null) {
checkBoxAr[4].setText("<html>" +
question.getOpE().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[4]);
}
if (question.getOpF() != null) {
checkBoxAr[5].setText("<html>" +
question.getOpF().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[5]);
}
if (question.getOpG() != null) {
checkBoxAr[6].setText("<html>" +
question.getOpG().replace("\n", "<br>") + "</html>");
jPanel1.add(checkBoxAr[6]);
}
}
jPanel1.repaint();
jPanel1.revalidate();
} catch (RemoteException ex) {
Logger.getLogger(TestPane.class.getName()).log(Level.SEVERE, null,
ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(TestPane.class.getName()).log(Level.SEVERE, null,
ex);
} catch (SQLException ex) {
Logger.getLogger(TestPane.class.getName()).log(Level.SEVERE, null,
ex);
}
}
Now the answers are loaded and visible in the panel.
I used the fill method to filling the check boxes on button action. In
this method, getting the QTemp object from map and fill the check boxes.
private void fill(String qId) {
QTemp temp = mpTemp.get(qId);
if (temp.getOpA() != null) {
checkBoxAr[0].setSelected(true);
}
if (temp.getOpB() != null) {
checkBoxAr[1].setSelected(true);
}
if (temp.getOpC() != null) {
checkBoxAr[2].setSelected(true);
}
if (temp.getOpD() != null) {
checkBoxAr[3].setSelected(true);
}
if (temp.getOpE() != null) {
checkBoxAr[4].setSelected(true);
}
if (temp.getOpF() != null) {
checkBoxAr[5].setSelected(true);
}
if (temp.getOpG() != null) {
checkBoxAr[6].setSelected(true);
}
}
In the next button I increment "q" value from one for every click. In this
action, I set the all check boxes states to the QTemp's opA, opB ... and I
put them in to QTemp to a mpTemp map with the question Id.
if (!mpTemp.containsKey(executeList.get(q).get(0).getqId())) {
mpTemp.put(executeList.get(q).get(0).getqId(), qTempAction);
}
try {
q++;
qCount++;
String question = executeList.get(q).get(0).getQuestion();
text.setText(question);
lbQCount.setText(qCount + " of " + executeList.size());
loadAnswers(executeList.get(q).get(0).getqId());
} catch (Exception e) {
e.printStackTrace();
q = executeList.size() - 1;
}
for (int i = 0; i < checkBoxAr.length; i++) {
checkBoxAr[i].setSelected(false);
}
try {
fill(executeList.get(q).get(0).getqId());
} catch (Exception e) {
}
In previous button I decrease the q's value from and put the QTemp object
to the map. And the problem is, If I press on next button first time and
then press previous and when I selected different check boxes, the result
on next previous is the first time result. How can I correct it with this
code.
Previous button Action
if (!mpTemp.containsKey(executeList.get(q).get(0).getqId())) {
mpTemp.put(executeList.get(q).get(0).getqId(), qTempAction);
}
try {
--q;
--qCount;
if (q == -1) {
q = 0;
}
if (qCount == 0) {
qCount = 1;
}
String question = executeList.get(q).get(0).getQuestion();
System.out.println(executeList.get(q).get(0).getqId());
text.setText(question);
lbQCount.setText(qCount + " of " + executeList.size());
loadAnswers(executeList.get(q).get(0).getqId());
for (int i = 0; i < checkBoxAr.length; i++) {
checkBoxAr[i].setSelected(false);
}
fill(executeList.get(q).get(0).getqId());
} catch (Exception e) {
e.printStackTrace();
}
As I know the problem is in creating QTemp objects. But I don't know to
fix it.
Thanks in Advance.
No comments:
Post a Comment