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

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...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...