Java Program to create JRadioButton from text


The following is an example to create JRadioButton from text −

Example

package my;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class SwingDemo {
   public static void main(String[] args) {
      JRadioButton radio1 = new JRadioButton("Cricket");
      JRadioButton radio2 = new JRadioButton("Football");
      ButtonGroup group = new ButtonGroup();
      group.add(radio1);
      group.add(radio2);
      radio1.setSelected(true);
      JFrame frame = new JFrame();
      frame.setLayout(new FlowLayout());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new JLabel("Fav Sports:"));
      frame.add(radio1);
      frame.add(radio2);
      frame.pack();
      frame.setVisible(true);
   }
}

Output

Updated on: 30-Jul-2019

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements