Java Program to create JCheckBox from text in Swing


The following is an example to create JCheckBox from text in Swing:

Example

import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingDemo {
   public static void main(String[] args) {
      JCheckBox checkBox1 = new JCheckBox("Cricket");
      JCheckBox checkBox2 = new JCheckBox("Squash");
      JCheckBox checkBox3 = new JCheckBox("Football");
      checkBox3.setSelected(true);
      JCheckBox checkBox4 = new JCheckBox("Hockey");
      JCheckBox checkBox5 = new JCheckBox("Fencing");
      JCheckBox checkBox6 = new JCheckBox("Tennis");
      JFrame frame = new JFrame();
      frame.setLayout(new FlowLayout());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new JLabel("Fav Sports? "));
      frame.add(checkBox1);
      frame.add(checkBox2);
      frame.add(checkBox3);
      frame.add(checkBox4);
      frame.add(checkBox5);
      frame.add(checkBox6);
      frame.pack();
      frame.setVisible(true);
   }
}

Output

Updated on: 30-Jul-2019

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements