Why does my button group doesnt work and display?

369 Views Asked by At

I am learning and new to GUI and in this program used ECLIPSE IDE (drag and drop windows builder)

I declared two radio buttons for Gender: Male and Female

If its clicks to male or female button it should only (click one) and go to that button and not both, so therefore I want it automatically the other one to be unselected to avoid duplicate selection

So I already did some research and import javax.swing.ButtonGroup; but I'm still confused as to why it doesn't work because I can still click both of the radio buttons. I think it has something to do with my panel?

Why does it happen and how can I fix it?

Here is my program

public AddRecord() {

    setUndecorated(true);
    setBackground(Color.WHITE);
    setVisible(true);
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setBounds(100, 100, 1063, 640);
    contentPane = new JPanel();
    contentPane.setBounds(new Rectangle(5, 0, 0, 0));
    contentPane.setBackground(new Color(255, 255, 240));
    contentPane.setBorder(new LineBorder(new Color(0, 0, 28), 1, true)); 
    setLocationRelativeTo(null);    
    setContentPane(contentPane);
    contentPane.setLayout(null);  
    
    JPanel pnlInfo = new JPanel();
    pnlInfo.setBounds(24, 20, 1015, 597);
    contentPane.add(pnlInfo);
    pnlInfo.setLayout(null);
    
    JLabel lblGender = new JLabel("Gender:");
    lblGender.setBounds(32, 174, 50, 10);
    pnlInfo.add(lblGender);
    lblGender.setFont(new Font("Tahoma", Font.PLAIN, 11));
    
    ButtonGroup btnBg = new ButtonGroup();  
    btnBg.add(rdbtnMale);
    btnBg.add(rdbtnFemale);     

    JRadioButton rdbtnMale  = new JRadioButton("Male");
    rdbtnMale.setBounds(79, 169, 55, 21);
    pnlInfo.add(rdbtnMale);

    JRadioButton rdbtnFemale = new JRadioButton("Female");
    rdbtnFemale.setBounds(136, 169, 76, 21);
    pnlInfo.add(rdbtnFemale); 
}
1

There are 1 best solutions below

0
JavaMan On BEST ANSWER

Your code does not even compile,

move the lines

ButtonGroup btnBg = new ButtonGroup();  
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);  

to the end of your code

like this

    setUndecorated(true);
    setBackground(Color.WHITE);
    setVisible(true);
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setBounds(100, 100, 1063, 640);
    JPanel contentPane = new JPanel();
    contentPane.setBounds(new Rectangle(5, 0, 0, 0));
    contentPane.setBackground(new Color(255, 255, 240));
    contentPane.setBorder(new LineBorder(new Color(0, 0, 28), 1, true));
    setLocationRelativeTo(null);
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel pnlInfo = new JPanel();
    pnlInfo.setBounds(24, 20, 1015, 597);
    contentPane.add(pnlInfo);
    pnlInfo.setLayout(null);

    JLabel lblGender = new JLabel("Gender:");
    lblGender.setBounds(32, 174, 50, 10);
    pnlInfo.add(lblGender);
    lblGender.setFont(new Font("Tahoma", Font.PLAIN, 11));


    JRadioButton rdbtnMale  = new JRadioButton("Male");
    rdbtnMale.setBounds(79, 169, 55, 21);
    pnlInfo.add(rdbtnMale);

    JRadioButton rdbtnFemale = new JRadioButton("Female");
    rdbtnFemale.setBounds(136, 169, 76, 21);
    pnlInfo.add(rdbtnFemale);

    ButtonGroup btnBg = new ButtonGroup();
    btnBg.add(rdbtnMale);
    btnBg.add(rdbtnFemale);