Skip to main content

JAVA AWT: Checkbox, Checkboxgroup

Java AWT Checkbox

The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on".

AWT Checkbox Class Declaration

  1. public class Checkbox extends Component implements ItemSelectable, Accessible  

Java AWT Checkbox Example

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

Java AWT Checkbox Example with ItemListener

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

ava 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 CheckboxGroup 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. }  

Anurag Rana Educator CSE/IT

Comments