Reputation: 1
I am working on my college project, where we have to design a program for a Sandwich shop. I am stuck on one place. I want to show the message box if nothing has been selected in buttongroup1
.
I tried this bit of code but it doesn't seem to be working correctly:
boolean ButtonGroup1 = false;
if (ButtonGroup1 == false)
JOptionPane.showMessageDialog (null, "Pleace Selete the Sandwiches order" );``
here is the program:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
/* Errors -------------------------------*/
buttonGroup1.add(HamRButton);
buttonGroup1.add(ChickenRButton);
buttonGroup1.add(CheeseRButton);
buttonGroup1.add(PorkRButton);
buttonGroup1.add(TunaRButton);
buttonGroup2.add(WaterRButton);
buttonGroup2.add(BottleofPOP);
buttonGroup2.add(CanofPoP);
buttonGroup3.add(CheeseExButton);
buttonGroup3.add(SaladExButton);
buttonGroup3.add(HamExButton);
buttonGroup3.add(TunaExButton);
buttonGroup3.add(ChickenExButton);
buttonGroup3.add(PorkExButton);
boolean buttonGroup1 = false;
if (!buttonGroup1){
JOptionPane.showMessageDialog(null, " ");
}
Upvotes: 0
Views: 2146
Reputation: 26209
Step1 : you need to iterate over all JRadioButtons
inside ButtonGroup
.
Step2 : cast the exach item of ButtonGroup
into JRadioButton
Step3: try to identify the each item ether is selected or not by calling isSelected()
function.
Step4: if any item in ButtonGroup is selected make the boolean variable buttonGroup1
to true
and quit the loop.
Step5: finally check the boolean
variable to display the Message
Try This:
boolean buttonGroup1 = false;
Enumeration<AbstractButton> allRadioButton=btngroup.getElements();
while(allRadioButton.hasMoreElements())
{
JRadioButton temp=(JRadioButton)allRadioButton.nextElement();
if(temp.isSelected())
{
buttonGroup1 =true;
break;
}
}
if (!buttonGroup1)
JOptionPane.showMessageDialog (null, "Pleace Selete the Sandwiches order" );
Upvotes: 1