Im going to create multiple choice question application using java swing.I have created swing class with list of radio buttons inside separate jPanels.I need to find the selected radio button and make highlighted on correct answer when submit the button. but I cant get the selected radio button list in swing. can anyone suggest me a good solution ?
private void myInitComponents() {
jLabel = new javax.swing.JLabel();
setLayout(new BorderLayout());
jButton1 = new javax.swing.JButton();
QuestionDaoIF questionDao = new QuestionDao();
List<Question> listOfQuestion = questionDao.getQuestion();
jPanel2 = new javax.swing.JPanel();
jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 100, 0, 300);
vbar.addAdjustmentListener(new MyAdjustmentListener());
add(jLabel, BorderLayout.CENTER);
jPanel2.setAutoscrolls(true);
List<String> answerList = new ArrayList<>();
List<Question> questionList = listOfQuestion ;
Collections.shuffle(questionList);
int i = 1;
for (Question question : questionList) {
QuestionPane pane = new QuestionPane();
pane.getjTextPane1().setText("("+i+") "+question.getQuestion());
//genarate random answers
genarateRandomAnswer(question, answerList, pane);
jPanel2.add(pane);
i++;
}
jButton1.setText("Submit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.add(jButton1);
//jPanel2.setBounds(100, 100, 800, 700);
this.add(getJMainScrollPane());
this.setTitle("Quizz");
this.setSize(1000, 700);
//pack();
}
private JScrollPane getJMainScrollPane() {
JScrollPane jMainScrollPane = new JScrollPane(jPanel2);
jMainScrollPane.setViewportBorder(BorderFactory
.createLineBorder(Color.GREEN));
jMainScrollPane
.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
return jMainScrollPane;
}
private void genarateRandomAnswer(Question question, List<String> answerList, QuestionPane pane) {
String answer1 = question.getCorrectAnswer();
String answer2 = question.getWrongAnswer1();
String answer3 = question.getWrongAnswer2();
String answer4 = question.getWrongAnswer3();
List<Answer> answrList = new ArrayList<>();
Answer ans1 = new Answer();
ans1.setAnswer(answer1);
ans1.setCorrectAnswer(true);
Answer ans2 = new Answer();
ans2.setAnswer(answer2);
ans2.setCorrectAnswer(false);
Answer ans3 = new Answer();
ans3.setAnswer(answer3);
ans3.setCorrectAnswer(false);
Answer ans4 = new Answer();
ans4.setAnswer(answer4);
ans4.setCorrectAnswer(false);
answrList.add(ans1);
answrList.add(ans2);
answrList.add(ans3);
answrList.add(ans4);
Collections.shuffle(answrList);
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup1.add(pane.getjRadioButton1());
buttonGroup1.add(pane.getjRadioButton2());
buttonGroup1.add(pane.getjRadioButton3());
buttonGroup1.add(pane.getjRadioButton4());
pane.getjRadioButton1().setText("(a) "+answrList.get(0).getAnswer());
pane.getjRadioButton1().setHideActionText(answrList.get(0).isCorrectAnswer());
pane.getjRadioButton2().setText("(b) "+answrList.get(1).getAnswer());
pane.getjRadioButton2().setHideActionText(answrList.get(1).isCorrectAnswer());
pane.getjRadioButton3().setText("(c) "+answrList.get(2).getAnswer());
pane.getjRadioButton3().setHideActionText(answrList.get(2).isCorrectAnswer());
pane.getjRadioButton4().setText("(d) "+answrList.get(3).getAnswer());
pane.getjRadioButton4().setHideActionText(answrList.get(3).isCorrectAnswer());
}
class MyAdjustmentListener implements AdjustmentListener {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
jLabel.setText(" New Value is " + e.getValue() + " ");
repaint();
}
}
true
to the correct answer and store it assetHideActionText()
– Acceleration