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.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »