Search code examples
javaswingjradiobutton

Java radio button selection and proceed


I want to have the user to select either one of the choices. but my code doesnt work. not sure if i got it wrong

if (!jRadioMale.isSelected() || !jRadioFemale.isSelected()) {
  JOptionPane.showConfirmDialog(rootPane, 
      "Please Select Male or Female", 
      "", 
      JOptionPane.OK_CANCEL_OPTION);
} else {
  // Continue to next
}   

it keeps prompting "Please select male or female" even if one is selected!


Solution

  • You need to do this ...

     if(jRadioMale.isSelected() || jRadioFemale.isSelected()){
        //Continue to next           
    
                }
        else{
         JOptionPane.showConfirmDialog(rootPane, "Please Select Male or Female", "", JOptionPane.OK_CANCEL_OPTION);
    
        }   
    

    Your code will always show the alert because Your condition returns true if any one of the button is not selected