Java Program to create JCheckBox from text in Swing

Java 8Object Oriented ProgrammingProgramming

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

raja
Published on 06-May-2019 07:29:43
Advertisements