-1

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();
    }
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
user3447091
  • 1
  • 1
  • 2

2 Answers2

0

don't know what you had tried before . If your code is not working then follow

http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

http://www.tutorialspoint.com/swing/swing_jradiobutton.htm

http://examples.javacodegeeks.com/desktop-java/swing/jradiobutton/get-selected-jradiobutton-from-buttongroup/

If-else condition within a JButton ActionEvent

package com.javacodegeeks.snippets.desktop;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class GetSelectedJRadioButtonFromButtonGroup extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JRadioButton java;
    private JRadioButton c;
    private JRadioButton net;
    private JButton button;
    private ButtonGroup buttonGroup;

    public GetSelectedJRadioButtonFromButtonGroup() {

        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());

        java = new JRadioButton("Java");
        java.setActionCommand("Java");

        c = new JRadioButton("C/C++");
        c.setActionCommand("c");

        net = new JRadioButton(".NET");
        net.setActionCommand("net");

        java.setSelected(true);

        button = new JButton("Check");

        button.addActionListener(this);

        buttonGroup = new ButtonGroup();

        //add radio buttons
        buttonGroup.add(java);
        buttonGroup.add(c);
        buttonGroup.add(net);

        add(java);
        add(c);
        add(net);
        add(button);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Check")) {
            System.out.println("Selected Radio Button: " + buttonGroup.getSelection().getActionCommand());
        }
    }

    private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new GetSelectedJRadioButtonFromButtonGroup();

  //Display the window.

  frame.pack();

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}

Source code from

Updated ans

boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false

try with this

You are using & instead this use && operator

& is Bitwise and,

&& is Logical and,

if (r1.isSelected() && r2.isSelected()) {
  JOptionPane.showMessageDialog(null,"JRadioButton 1 and JRadioButton 2  are Selected ");
} 
Community
  • 1
  • 1
  • how can i fix my problem ?!i have seen lot of tutorials why i cant do like this if (r1.isSelected()& r2.isSelected()) what is my mistake because program working and the problem it skips the if condition and goes for else only – user3447091 May 10 '14 at 06:50
  • thanks but the only condition works fine is only when else if (r3.isSelected() && r4.isSelected()) and other still have problem even when i added && ?why is that ?! – user3447091 May 10 '14 at 07:06
0

because the and symbol you're using is wrong! Use && for a boolean condition between two expressions and & only for bitwise operation

  • i have tried this && and this have solved the problem .thanks zicow56 – user3447091 May 10 '14 at 06:51
  • your groups are wrong! As you have defined them it works only if you select r3 and r4. Have a look at the conditions too, you are checking if r1 and r2 are selected at the same time, but it never happen because they belong to the same group –  May 10 '14 at 07:09