AWT CheckboxGroup

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java AWT CheckboxGroup

The object of CheckboxGroup class is used to group together a set of Checkbox. At a time only one check box button is allowed to be in "on" state and remaining check box button in "off" state. It inherits the object class.

Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special control for creating radio buttons in AWT.

AWT CheckboxGroup Class Declaration

  1. public class CheckboxGroup extends Object implements Serializable  

Java AWT Check box Group Example

  1. import java.awt.*;    
  2. public class CheckboxGroupExample    
  3. {    
  4.        CheckboxGroupExample(){    
  5.        Frame f= new Frame("CheckboxGroup Example");    
  6.         CheckboxGroup cbg = new CheckboxGroup();  
  7.         Checkbox checkBox1 = new Checkbox("C++", cbg, false);    
  8.         checkBox1.setBounds(100,10050,50);    
  9.         Checkbox checkBox2 = new Checkbox("Java", cbg, true);    
  10.         checkBox2.setBounds(100,15050,50);    
  11.         f.add(checkBox1);    
  12.         f.add(checkBox2);    
  13.         f.setSize(400,400);    
  14.         f.setLayout(null);    
  15.         f.setVisible(true);    
  16.      }    
  17. public static void main(String args[])    
  18. {    
  19.     new CheckboxGroupExample();    
  20. }    
  21. }  

Java AWT Check box Group Example with Item Listener

  1. import java.awt.*;    
  2. import java.awt.event.*;  
  3. public class CheckboxGroupExample    
  4. {    
  5.      CheckboxGroupExample(){    
  6.        Frame f= new Frame("CheckboxGroup Example");    
  7.        final Label label = new Label();          
  8.        label.setAlignment(Label.CENTER);  
  9.        label.setSize(400,100);  
  10.         CheckboxGroup cbg = new CheckboxGroup();  
  11.         Checkbox checkBox1 = new Checkbox("C++", cbg, false);    
  12.         checkBox1.setBounds(100,10050,50);    
  13.         Checkbox checkBox2 = new Checkbox("Java", cbg, false);    
  14.         checkBox2.setBounds(100,15050,50);    
  15.         f.add(checkBox1); f.add(checkBox2); f.add(label);    
  16.         f.setSize(400,400);    
  17.         f.setLayout(null);    
  18.         f.setVisible(true);    
  19.         checkBox1.addItemListener(new ItemListener() {  
  20.             public void itemStateChanged(ItemEvent e) {               
  21.                label.setText("C++ checkbox: Checked");  
  22.             }  
  23.          });  
  24.         checkBox2.addItemListener(new ItemListener() {  
  25.             public void itemStateChanged(ItemEvent e) {               
  26.                label.setText("Java checkbox: Checked");  
  27.             }  
  28.          });  
  29.      }    
  30. public static void main(String args[])    
  31. {    
  32.     new CheckboxGroupExample();    
  33. }    
  34. }