Example usage for javax.swing JRadioButton setFocusable

List of usage examples for javax.swing JRadioButton setFocusable

Introduction

In this page you can find the example usage for javax.swing JRadioButton setFocusable.

Prototype

public void setFocusable(boolean focusable) 

Source Link

Document

Sets the focusable state of this Component to the specified value.

Usage

From source file:lab4.YouQuiz.java

private void updateAnswerForm() {
    contentPanel.answerPanel.removeAll();

    final Question question = questionsArray.get(questionIndex);

    switch (question.type) {
    case Question.QUESTION_TYPE_MULTIPLE_CHOICE:
    case Question.QUESTION_TYPE_TRUE_FALSE:
        JRadioButton radioButton;
        final ButtonGroup radioGroup = new ButtonGroup();

        for (int i = 0; i < ((MultipleChoiceQuestion) question).getChoices().size(); ++i) {
            radioButton = new JRadioButton(((MultipleChoiceQuestion) question).getChoices().get(i));
            radioButton.setFont(new Font("Consolas", Font.PLAIN, 20));
            radioGroup.add(radioButton);
            radioButton.setFocusable(false);
            contentPanel.answerPanel.add(radioButton);
        }//from  ww  w.ja  va2 s  .c  o  m

        for (ActionListener al : contentPanel.checkButton.getActionListeners()) {
            contentPanel.checkButton.removeActionListener(al);
        }

        contentPanel.checkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (question.checkAnswer(getSelectedButtonText(radioGroup))) {
                    JOptionPane.showMessageDialog(null, "Thats Great, Correct Answer", "Excellent",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Oops! Wrong Answer. Its '" + question.getAnswer().get(0) + "'", "Sorry",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        break;
    case Question.QUESTION_TYPE_SHORT_ANSWER:
        final JTextField answerText = new JTextField(25);
        answerText.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
        contentPanel.answerPanel.add(answerText);

        for (ActionListener al : contentPanel.checkButton.getActionListeners()) {
            contentPanel.checkButton.removeActionListener(al);
        }

        contentPanel.checkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (question.checkAnswer(answerText.getText())) {
                    JOptionPane.showMessageDialog(null, "Thats Great, Correct Answer", "Excellent",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Oops! Wrong Answer. Its '" + question.getAnswer().get(0) + "'", "Sorry",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        break;
    }

    contentPanel.answerPanel.invalidate();
}