Java Swing How to - Create JCheckBox from text








Question

We would like to know how to create JCheckBox from text.

Answer

//from   w ww . jav a 2  s.c  o m
import java.awt.FlowLayout;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {

  public static void main(String[] args) {
    JCheckBox a = new JCheckBox("A");
    a.setSelected(true);
    JCheckBox b = new JCheckBox("B");
    JCheckBox c = new JCheckBox("C");
    JCheckBox d = new JCheckBox("D");

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JLabel("Car Features"));
    frame.add(a);
    frame.add(b);
    frame.add(c);
    frame.add(d);
    frame.pack();
    frame.setVisible(true);
  }
}