简体   繁体   中英

Get text from JRadioButton that isn't part of a ButtonGroup

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM