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
Krantik Chavan

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

Advertisements

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