i have small condition problem ,when i click a JRadiobutton it doesn't check the condition in if else statement and goes directly to else statement .
this is my code i have do with the condition of if else and every thing is working Except the if else it skip to else only
package javaapplication4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class RadioApplication extends JFrame implements ItemListener {
JRadioButton r1 = new JRadioButton("JRadioButton 1");
JRadioButton r2 = new JRadioButton("JRadioButton 2");
JRadioButton r3 = new JRadioButton("JRadioButton 3");
JRadioButton r4 = new JRadioButton("JRadioButton 4");
JRadioButton r5 = new JRadioButton("JRadioButton 5");
JRadioButton r6 = new JRadioButton("JRadioButton 6");
ButtonGroup g1 = new ButtonGroup();
ButtonGroup g2 = new ButtonGroup();
RadioApplication() {
setTitle("Payment Mode");
setSize(300, 250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1));
g1.add(r1);
g2.add(r2);
g1.add(r3);
g2.add(r4);
g1.add(r5);
g2.add(r6);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
r4.addItemListener(this);
r5.addItemListener(this);
r6.addItemListener(this);
add(r1);
add(r2);
add(r3);
add(r4);
add(r5);
add(r6);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (r1.isSelected() && r2.isSelected()) {
JOptionPane.showMessageDialog(null, "JRadioButton 1 and JRadioButton 2 are Selected ");
} else if (r3.isSelected() && r4.isSelected()) {
JOptionPane.showMessageDialog(null, "JRadioButton 3 and JRadioButton 4 are Selected");
} else if (r5.isSelected() && r6.isSelected()) {
JOptionPane.showMessageDialog(null, "JRadioButton 5 and JRadioButton 6 are Selected");
} else {
JOptionPane.showMessageDialog(null, "No JRadioButton was Selected");
}
}
public static void main(String[] args) {
RadioApplication app1 = new RadioApplication();
}
}