0

This code not work properly. When I select male, every time it select female automatically?

How to validate radio button for gender?

    rbtnmale = new JRadioButton("male");
    rbtnmale.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        gender="male";
        rbtnmale.setSelected(true);
        rbtnmale.setSelected(false);
    }
});
rbtnmale.setBounds(116, 127, 58, 23);
frame.getContentPane().add(rbtnmale);

rbtnfemale = new JRadioButton("female");
rbtnfemale.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        gender="female";
        rbtnfemale.setSelected(true);
        rbtnmale.setSelected(false);

    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Njena
  • 191
  • 3
  • 11
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 16 '18 at 07:26

2 Answers2

2

You should be using ButtonGroup, put all of your JRadioButtons in it and then loop through buttonGroup.getElements() to find which JRadioButton is selected. Here is a complete example:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Enumeration;
import javax.swing.AbstractButton;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class Example {

    ButtonGroup group = new ButtonGroup();//used to store all radio buttons.

    public Example() {
        initComponents();
        currentSelectedOption();
    }

    private void initComponents() {
        //Radio buttons
        JRadioButton female = new JRadioButton("Female");
        JRadioButton male = new JRadioButton("Male");
        JRadioButton other = new JRadioButton("Other");

        female.setSelected(true);//by default, select female.

        //Add all radio buttons to a group. 
        //It will allow to only have one selected at a time.
        group.add(male);
        group.add(female);
        group.add(other);

        //Add your components to a panel, it's a good practice.
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(female);
        panel.add(male);
        panel.add(other);

        JFrame frame = new JFrame("Example");
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);

        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * Getting the current selected radio button.
     *
     * @return Text of the radio button currently selected.
     */
    public String getSelectedOption() {
        Enumeration<AbstractButton> radioButtons = group.getElements();
        while (radioButtons.hasMoreElements()) {
            AbstractButton currentRadioButton = radioButtons.nextElement();
            if (currentRadioButton.isSelected()) {
                return currentRadioButton.getText();
            }
        }
        return null;
    }

    private void currentSelectedOption() {
        String selected = getSelectedOption();
        if (selected == null) {
            System.out.println("There is something wrong! Nothing is selected");
            return;
        }
        switch (selected.toLowerCase()) {
            case "male":
                System.out.println("male is selected");
                break;
            case "female":
                System.out.println("female is selected");
                break;
            case "other":
                System.out.println("other is selected");
                break;
        }

    }
}

public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Example();
                }
            });
        }
M. Al Jumaily
  • 731
  • 1
  • 6
  • 21
0
    rbtnmale.setSelected(true);
    rbtnmale.setSelected(false);

Based on your current code you want:

    //rbtnmale.setSelected(false);
    rbtnfemale.setSelected(false);

However you don't even need the above code in the ActionListener for either of the radio buttons.

Instead you should be using a ButtonGroup and the ButtonGroup will manage the selected state of each radio button for you:

ButtonGroup group = new ButtonGroup();
group.add(btnMale);
group.add(btnFemale);

Read the section from the Swing tutorial on How Use Buttons for more information

camickr
  • 321,443
  • 19
  • 166
  • 288