Set JCheckBox to selected in Java

Description

The following code shows how to set JCheckBox to selected.

Example


import java.awt.FlowLayout;
/*w  w  w . j  a  v a 2 s  .c  om*/
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {

  public static void main(String[] args) {

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

    JCheckBox ac = new JCheckBox("A/C");
    ac.setSelected(true);
    
    JCheckBox cdPlayer = new JCheckBox("A");
    JCheckBox cruiseControl = new JCheckBox("B");
    JCheckBox keylessEntry = new JCheckBox("C");
    JCheckBox antiTheft = new JCheckBox("D");
    JCheckBox centralLock = new JCheckBox("E");

    frame.add(new JLabel("Car Features"));
    frame.add(ac);
    frame.add(cdPlayer);
    frame.add(cruiseControl);
    frame.add(keylessEntry);
    frame.add(antiTheft);
    frame.add(centralLock);
    frame.pack();
    frame.setVisible(true);
  }
}

The code above generates the following result.

Set JCheckBox to selected in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer