Mutually Exclusive Three Choices Represented By Three JRadioButtons - Java Swing

Java examples for Swing:JRadioButton

Description

Mutually Exclusive Three Choices Represented By Three JRadioButtons

Demo Code

import java.awt.BorderLayout;
import java.awt.Container;

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

public class Main extends JFrame {
  ButtonGroup genderGroup = new ButtonGroup();
  JRadioButton genderMale = new JRadioButton("Male");
  JRadioButton genderFemale = new JRadioButton("Female");
  JRadioButton genderUnknown = new JRadioButton("Unknown");

  public Main() {
    setTitle("Mutually Exclusive JRadioButtons Group");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    genderGroup.add(genderMale);/*from   ww w.  j a v  a  2s . c o m*/
    genderGroup.add(genderFemale);
    genderGroup.add(genderUnknown);

    Box b1 = Box.createVerticalBox();
    b1.add(genderMale);
    b1.add(genderFemale);
    b1.add(genderUnknown);

    Container contentPane = this.getContentPane();
    contentPane.add(b1, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    Main bf = new Main();
    bf.pack();
    bf.setVisible(true);
  }
}

Related Tutorials