Selected RadioButton on ButtonGroup doesn't show into TextArea in Java

460 views Asked by At

I'm a student and right now I have a project for my college. this is short of my code.

private void buttonProccesActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String gender = String.valueOf(buttongroupGender.getSelection());
    //the problem is right over top of this comment

    String date = String.valueOf(comboboxDate.getSelectedItem());
    String month = String.valueOf(comboboxMonth.getSelectedItem());
    String year = String.valueOf(comboboxYear.getSelectedItem());

    textareaWrite.setText("");
    textareaWrite.append("\nName : " + textfieldName.getText());
    textareaWrite.append("\n" + gender);
    textareaWrite.append("\nBirth : " + date);
    textareaWrite.append(" - " + month);
    textareaWrite.append(" - " + year);
    textareaWrite.append("\nBirth place : " + textfieldBirthPlace.getText());  

}

Everything, every code is working perfectly except the button group.

String gender = String.valueOf(buttongroupGender.getSelection());

I can't get the value of radio button that is selected in button group that has the value of Boy and Girl. Is there any solution for this ?

1

There are 1 answers

0
UkFLSUI On BEST ANSWER

To solve your problem you have to add setActionCommand to all the JRadioButton during their creation.

Example:

I assume that you have already created two JRadioButton namely boy and girl in the constructor part. There you also have to include two more lines each after the JRadioButton as below:

boy = new JRadioButton("Boy");
java.setActionCommand("Boy");
girl = new JRadioButton("Girl");
c.setActionCommand("Girl");

Now, in your buttonProccesActionPerformed() method (the code which you have provided), do the following edit:

String gender = buttongroupGender.getSelection().getActionCommand();