-1

My issue is that I do not know how to check whether or not a radio button is selected, and then choose a different output according to which one i selected. Basically, as my code is right now, once a radiobutton is selected, it is selected forever, it seems. how do i fix my code to send a different output according to what button is selected? Here is my code...

JRadioButton radioButton1;
JRadioButton radioButton2;
JRadioButton radioButton3;
JRadioButton radioButton4;
int button = 1;

....

private void createCourses(){

    JPanel eastPanel = new JPanel(new GridLayout(5, 1, 10, 10));

    eastPanel.setBounds(250, 50, 150, 120);
    eastPanel.setBorder(raisedetched);
    ButtonGroup radio = new ButtonGroup();

    JLabel label1 = new JLabel("Course offerings");
    radioButton1 = new JRadioButton();
    radioButton2 = new JRadioButton();
    radioButton3 = new JRadioButton();
    radioButton4 = new JRadioButton();

    eastPanel.add(label1);
    eastPanel.add(radioButton1);
    eastPanel.add(radioButton2);
    eastPanel.add(radioButton3);
    eastPanel.add(radioButton4);

    radio.add(radioButton1);
    radio.add(radioButton2);
    radio.add(radioButton3);
    radio.add(radioButton4);

    radioButton1.addItemListener(this);
    radioButton2.addItemListener(this);
    radioButton3.addItemListener(this);
    radioButton4.addItemListener(this);

    radioButton1.setSelected(true);

    radio.getSelection();
    contentPane.add(eastPanel);
    setVisible(true); 
    }

   public void itemStateChanged(ItemEvent e) {
   if(radioButton1.isSelected() == true){
    button = 1;
    myStats.setCourseOfferings(button);
}
else{
    radioButton1.setSelected(false);
}


if(radioButton2.isSelected() == true){
    button = 2;
    myStats.setCourseOfferings(button);
}

if(radioButton3.isSelected() == true){
    button = 3;
    myStats.setCourseOfferings(button);
}

if(radioButton4.isSelected() == true){
    button = 4;
    myStats.setCourseOfferings(button);
}
 }
 }

Thank you and any help is appreciated.

Jacob Stinson
  • 181
  • 1
  • 16
  • If [`isSelected()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#isSelected%28%29) is not the answer, I'd have to conceded I do not understand the question. ..As an aside, there **is** no question here. What is your question? Also: 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 29 '13 at 15:00
  • My question is, how do i fix my code to send a different output according to what button is selected. as of now, isSelected() does not work. – Jacob Stinson Nov 29 '13 at 15:05
  • *"how do i fix my code to send a different output according to what button is selected"* So add a '?' at the end of that, and [edit it into the question](http://stackoverflow.com/posts/20288436/edit). Oh, and let me know in a comment when there is an SSCCE.. – Andrew Thompson Nov 29 '13 at 15:07
  • 1
    I added the question, and i trimmed down the code...is this good? – Jacob Stinson Nov 29 '13 at 15:11
  • *"trimmed down the code"* 'trimming code' is not the problem that stops that from being an SSCCE. It is the other 4 letters of the SSSCCE your should be fulfilling. Please actually ***read*** the linked document. If there is anything in it you do not understand, ask. I am well placed to explain, since I am the author of it. – Andrew Thompson Nov 29 '13 at 15:15
  • Okay, i added more to the question...I believe that everything that I have included is either necessary information, or could potentially be important information. – Jacob Stinson Nov 29 '13 at 15:25
  • That load of code snippets does not even compile! Good luck with it. – Andrew Thompson Nov 29 '13 at 15:49
  • 1
    well it does compile if i include all of the code, but according to you i can't do that – Jacob Stinson Nov 29 '13 at 15:52
  • Look. Get something straight. That code has 4 `JRadioButton` instances. An example requires only 2. Changing that in itself would would shrink the code seen by half. *Is that not obvious to you?* It is the 3rd and 4th buttons that should be removed, not the imports, not the `main(String[])`.. The point of an SSCCE is that we should be able to copy/paste/compile/run to see the exact state of the current code (in a minimal form) that you do. If you cannot understand that, then I can be of no further help. – Andrew Thompson Nov 29 '13 at 15:57

2 Answers2

1

I would suggest you this approach:

  • Have a variable that keeps the selected ID or number or whatever you want to pass as argument to myStats.setCourseOfferings() method.

  • Implement just one ActionListener to listen to action events coming from your JRadioButtons. Attach this listener to each radio button. Look at the tutorial: How to Use Buttons, Check Boxes, and Radio Buttons for further information.

  • Use JComponent.putClientProperty() method to "attach" a value to each radio button.

  • Use JCOmponent.getClientProperty() method to retrieve this value and update the variable defined in the first step.

Example (it's not SSCCE, just an example)

Integer selectedRadioButton = -1; //declared as class variable
final String COURSE_ID = "CourseID"; //declared class variable

...

ActionListener actionListener = new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
       if(e.getSource() instanceof JRadioButton) {
           JRadioButton radioButton = (JRadioButton)e.getSource();
           selectedRadioButton = (Integer)radioButton.getClientProperty(COURSE_ID);
       }
   }
};

JRadioButton radioButton1 = new JRadioButton("Radio 1");
radioButton1.putClientProperty(COURSE_ID, 1);
radioButton1.addActionListener(actionListener);

JRadioButton radioButton2 = new JRadioButton("Radio 2");
radioButton2.putClientProperty(COURSE_ID, 2);
radioButton2.addActionListener(actionListener);

// and so on

JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        myStats.setCourseOfferings(selectedRadioButton);
    }
});
dic19
  • 17,821
  • 6
  • 40
  • 69
0

Why are you using addItemListenerinstead of addActionListener on the JRadioButtons ?

Besides that, there is a very similar program here which works, you may want to see how is it different from yours: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton

Daniel
  • 21,933
  • 14
  • 72
  • 101