help, my questions are:

  1. why isn't itemStateChanges triggered, I tried to put it in the inner class ButtonHandler and also in RadioButtonHandler Im having trouble with it, what is the right way to do it? I want to trigger and check the marked JRadioButtons after the user click the "check" button.

  2. What is the right way to check which button was clicked, I feel like comparing the strings is bad programming practise. Maybe using an ID ?

  3. How should I make a "reset" button(start over), I want to uncheck all radio buttons and run the constructor once again.

Thank you for your help !

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;
    public class ExamFrame extends JFrame {

        static ArrayList<Question> qArrList;
        JRadioButton a1,a2,a3,a4;
        public ExamFrame() {
            super("Quiz");

            setLayout(new GridLayout(0, 1));
            GridBagConstraints gbc = new GridBagConstraints();

            Exam exam = new Exam();
            qArrList = exam.getExam();
            int count=0;
            for(Question q : qArrList){
                count++;
                JLabel questionLabel = new JLabel(count+". "+q.getQustion()); //swing constant ?
                ArrayList<String> ansRand = q.getAllRandomAns();
                a1 = new JRadioButton(ansRand.get(0)); 
                a2 = new JRadioButton(ansRand.get(1));
                a3 = new JRadioButton(ansRand.get(2));
                a4 = new JRadioButton(ansRand.get(3));
                add(questionLabel);
                add(a1);add(a2,gbc);add(a3);add(a4);

                ButtonGroup radioGroup = new ButtonGroup(); //logical relationship
                radioGroup.add(a1);radioGroup.add(a2);radioGroup.add(a3);radioGroup.add(a4);
            }
            //buttons:
            JButton checkMe = new JButton("Check Exam");
            JButton refresh = new JButton("Start Over");
            ButtonHandler handler = new ButtonHandler();
            checkMe.addActionListener(handler);
            refresh.addActionListener(handler);
            add(checkMe);
            add(refresh);

        }
            /** Listens to the radio buttons. */
            public class ButtonHandler implements ActionListener
            {
                public void actionPerformed (ActionEvent e) {
                    if(e.getActionCommand().equals("Start Over")){ //id?
                        //how to do this?

                    }
                    else{
                        RadioButtonHandler handler = new RadioButtonHandler();
                        a1.addItemListener(handler);
                        System.out.println("success?");
                    }
                    JOptionPane.showMessageDialog(ExamFrame.this, String.format("You pressed: %s", e.getActionCommand()));
                }
                public void itemStateChanged(ItemEvent e) //can i add it here?
                {
                    JOptionPane.showMessageDialog(ExamFrame.this, String.format("yes?"));
                    System.out.println("success!");
                }

            }
            public class RadioButtonHandler implements ItemListener
            {
                public void itemStateChanged(ItemEvent e)
                {
                    JOptionPane.showMessageDialog(ExamFrame.this, String.format("radio state changed"));

                }

            }
    }
有帮助吗?

解决方案

why "itemStateChanges" isn't triggered, i tried to put it in the inner class "ButtonHandler" and also in "RadioButtonHandler" Im having troubles with it, what is the right way to do it? I want to trigger and check the marked JRadioButtons after the user click the "check" button.

ButtonHandler is implemented with ActionListener only:

  public class ButtonHandler implements ActionListener{}

The itemStateChanged(ItemEvent) function belongs to ItemListener. This function is triggered if state of a source component to which this listener is registered gets changed. So implement the ItemListener. However, one more thing to note, that JButton doesn't respond to ItemListener but JRadioButton will. Because this Item events are fired by components that implement the ItemSelectable interface. Some example of such components are: check boxes, check menu items, toggle buttons and combo boxes including Radio Buttons as mentioned above.


What is the right way to check which button was clicked, i feel like comparing the strings is wrong programming. Maybe using an ID

Well using the event source function: e.getSource(), check whither the type of the source is your expected type and cast it to appropriate type. And then you can use getName(String) function and check the name you were expecting. Of-course you should assign the name using setName(String) after initialization of component. Or using the component reference directly if it is declared in the Class context and you have direct access to the component.

        @Override
        public void itemStateChanged(ItemEvent e) {

            if(e.getSource() instanceof JCheckBox)
            { 
                JCheckBox  checkBox = (JCheckBox)e.getSource();
                if(checkBox.getName().equals("expectedName"))
                          ; // do my thing
             }
        }

How should i make a "reset" button(start over), i want to uncheck all radio buttons and run the constructor once again.

Well you are working with ButtonGroup. And ButtonGroup has a nice function: clearSelection() to help with whatever(I could not understand the part: run the constructor part) you want.

Edit: As you wanted me to see an ItemListener implemented class, Yes i can see that But:

  1. i can not see that you have actually registered an instance of that class(a1.addItemListener(handler);) to any component before performing any action on the component to which ButtonHandler is registered to: checkMe, refresh
  2. In addition to that, in this action performed function, you are checking with action command, which you haven't even set with JButton.setActionCommand(String) function. You should not assign a (Item)listener depending on event-occurrence of another (Action)listener.

Tutorial:

  1. How to Write an ItemListener
  2. How to Write an ActionListener
  3. How to Use the ButtonGroup Component
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top