1

My program is a GUI. I have this method where when a button is clicked. It populates the next screen with JRadioButtons dynamically.

private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt)
 {                 
        if(evt.getActionCommand().equals("Set Exam"))
        {
            CardLayout cL = (CardLayout)cardPanels.getLayout();
            cL.show(cardPanels, "setExamPanel");
        }

        try
        {
            //InputStream code

            String theMessage = myObject.getMessage();          

            String delims = "(?=(0*([0-9]{1,2}|100)))"; 
            String[] questions = theMessage.split(delims);

            System.out.println(Arrays.toString(questions));         

            for (int j = 1; j < questions.length; j++)
            {
                settingQuestionBoxes = new JCheckBox(questions[j]);             

                settingQuestionTextField = new JTextField("");

                jPanel1.add(settingQuestionBoxes);              
                jPanel1.add(settingQuestionTextField);
                jPanel1.revalidate();
                jPanel1.repaint();                  

            }

            //close streams and socket code

        }
        catch(Exception e)
        {
            System.out.println(e);
        }
 }

Then I have this other method from another screen where the data that is populated from the previous method goes to.

private void setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
            if(evt.getActionCommand().equals("Set Exam Question"))
            {           
                        ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>();

                        for(JToggleButton questions: settingQuestionBoxes)
                        {               
                            if(questions.isSelected())
                            {               
                                System.out.println(questions.getActionCommand());
                            }
                        }           

                        CardLayout cL = (CardLayout)cardPanels.getLayout();
                        cL.show(cardPanels, "instructorPanel");
             }              
     }

So basically when i call this System.out.println(questions.getActionCommand()) I'm trying to see the text from the JRadiobutton that was clicked on. Right now when I run the program and select a button. Nothing happens.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
C-Elipse
  • 43
  • 4
  • 1) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 14 '13 at 22:51

1 Answers1

3

Put the buttons into a List<JToggleButton> such as an ArrayList<JToggleButton> and then iterate through the list when the information is needed.

for (JToggleButton btn : myButtonList) {
   if (btn.isSelected() {
     String actionCommand = btn.getActionCommand();
     // use the actionCommand here
   }
}

Note that JToggleButton is the parent class for JRadioButton and using it would allow you to add JRadioButtons, JCheckBoxes, and JToggleButtons to the list. Since your JRadioButton is not part of a ButtonGroup, perhaps you should be using a JCheckBox instead.


Edit

You now have posted this code, stating it doesn't work:

// Section (A)
ArrayList<JToggleButton> settingQuestionButton = new ArrayList<JToggleButton>();

// Section (B)
for(JToggleButton questions: settingQuestionButon)  
{               
    if(questions.isSelected())
    {               
        System.out.println(questions.getActionCommand());
    }
}

Is this code, both (A) and (B), all together in your program? If so, it would make sense that it doesn't work. You should have (A) in a constructor or some set up method. You should follow (A) with code that creates your JRadioButtons or JCheckBoxes, that sets their actionCommand String, that places them in the GUI, and that adds them to the ArrayList.

The part (B) code, the enhanced for loop would need to be in code that is called in response to an event, perhaps in a JButton or radio button's ActionListener.

Please check out this information and fill us in on the details. Please consider creating and posting an sscce illustrating your problem for us.


Edit 2
Your code is confusing in that you appear to have two completely variables of different types with the exact same name, and you appear to be assuming that this will give the variable magical properties that will allow it to know what it's "twin" might be doing. Java doesn't work that way, and in fact variable names are not nearly all that important or smart to allow them any such functionality. Rather your code must be smart. I'm assuming that more than one of your JCheckBoxes will be checked, and that you want to check which ones are checked at some point in your program. If so, then in your class you should have a List or ArrayList field, something like

private List<JToggleButton> questionsList = new ArrayList<JToggleButton>();

This way this field will available throughout the class.

Then where you create your JCheckBoxes, you add them to this list:

  private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt)
   {                 
       if(evt.getActionCommand().equals("Set Exam"))
       {
           CardLayout cL = (CardLayout)cardPanels.getLayout();
           cL.show(cardPanels, "setExamPanel");
       }

       try
       {
           String theMessage = myObject.getMessage();          

           String delims = "(?=(0*([0-9]{1,2}|100)))"; 
           String[] questions = theMessage.split(delims);

           for (int j = 1; j < questions.length; j++)
           {
               settingQuestionBox = new JCheckBox(questions[j]);  // *** renamed to make more sense
               settingQuestionBox.setActionCommand(questions[j]);  // **** add actionCommand String
               questionsList.add(settingQuestionBox); // ****** add JCheckBox to List

               settingQuestionTextField = new JTextField("");

               jPanel1.add(settingQuestionBox);              
               jPanel1.add(settingQuestionTextField);
               jPanel1.revalidate();
               jPanel1.repaint();                  

           }

           //close streams and socket code

       }
       catch(Exception e)
       {
           // System.out.println(e);
           e.printStackTrace(); // ***** more informative
       }
   }

Then elsewhere in your code

  setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
  {
     if(evt.getActionCommand().equals("Set Exam Question"))
     {           
        // ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>();

        for(JToggleButton questions: questionsList)
        {               
            if(questions.isSelected())
            {               
                System.out.println(questions.getActionCommand());
            }
        }           

        CardLayout cL = (CardLayout)cardPanels.getLayout();
        cL.show(cardPanels, "instructorPanel");
      }
  }    

And of course you'll need to take care that the ActionListener is added to a button

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • ArrayList settingQuestionButton = new ArrayList(); for(JToggleButton questions: settingQuestionButton) { if(questions.isSelected()) { System.out.println(questions.getActionCommand()); } } This isn't working @Hovercraft Full Of Eels – C-Elipse Dec 14 '13 at 23:49
  • @C-Elipse: "isn't working" doesn't give me enough information to allow me to understand what might be wrong. Please consider editing your original question and adding any new or changed code to the bottom as well as any error messages or descriptions of new problems. Don't delete your old code. Also, remember that when creating JRadioButtons and JCheckBoxes, you usually have to explicitly set the actionCommand text. – Hovercraft Full Of Eels Dec 15 '13 at 01:48
  • 1
    Another thing I noticed is OP never actually adds any radio buttons to the list. And other thing that confuses me is the usage of `settingQuestionButton` variable. The first part of OP question it is using as a radio button object, but in the second part, it is also used as a list. – Paul Samsotha Dec 15 '13 at 02:50
  • @C-Elipse: please re-read my answer as you don't seem to be following my suggestions. Please ask for clarification of anything that you don't understand. – Hovercraft Full Of Eels Dec 15 '13 at 03:14
  • @Hovercraft Full Of Eels: When you say "You should follow (A) with code that creates your JRadioButtons or JCheckBoxes, that sets their actionCommand String, that places them in the GUI, and that adds them to the ArrayList." I'm confused about that part, I thought that this: settingQuestionBoxes = new JCheckBox(questions[j]) - in the first section of my code already does that. Could you please clarify. – C-Elipse Dec 15 '13 at 03:28
  • @C-Elipse: see **Edit 2** to answer. – Hovercraft Full Of Eels Dec 15 '13 at 03:44
  • @Hovercraft Full Of Eels: Thank you very much for the help, It's working perfectly now. – C-Elipse Dec 15 '13 at 03:57
  • @C-Elipse: You're welcome-- glad you've got it working. – Hovercraft Full Of Eels Dec 15 '13 at 03:58
  • put/getClientProperty without loop, but +1 – mKorbel Dec 15 '13 at 09:34