javacodex.com
Java Examples
Java Examples
memu home questions

JCheckBox

A JCheckBox also has two states: selected and unselected. A group of JCheckBoxes is used when the user can select zero or more choices from a list of two or more choices. One JCheckBox is used when the user has a boolean choice to indicate true or false.

Source: (JCheckBoxExample.java)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class JCheckBoxExample {
 
  public static void main(String[] arguments) {
 
    final JLabel label = new JLabel("UnChecked");
 
    JCheckBox check = new JCheckBox("Show title", false);
    check.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        int sel = e.getStateChange();
        if (sel==ItemEvent.SELECTED) {
          label.setText("Checked");
        } else {
          label.setText("UnChecked");
        }
      }
    });
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame("JColorChooser Example");
    f.setLayout(new FlowLayout());
    f.setSize(250,150);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    f.add(check);
    f.add(label);
 
    f.setVisible(true);
  }
}
 

Output:

$java JCheckBoxExample
Output



Contact: javacodex@yahoo.com