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

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...