Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
karthikeya Boyini

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.