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

locking

DBMS Locking Part I (DBMS only) TECHNICAL ARTICLES -> PERFORMANCE ARTICLES [  Back  ] [  Next  ] DBMS is often criticized for excessive locking – resulting in poor database performance when sharing data among multiple concurrent processes. Is this criticism justified, or is DBMS being unfairly blamed for application design and implementation shortfalls? To evaluate this question, we need to understand more about DBMS locking protocols. In this article, we examine how, why, what and when DBMS locks and unlocks database resources. Future articles will address how to minimize the impact of database locking. THE NEED FOR LOCKING In an ideal concurrent environment, many processes can simultaneously access data in a DBMS database, each having the appearance that they have exclusive access to the database. In practice, this environment is closely approximated by careful use of locking protocols. Locking is necessary in a concurrent environment to as...

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE Customers exploring the field of business intelligence for the first time often lead with: What is the difference between a data warehouse and a data mart? The next question follows as predictably as night follows day: which one does my company need? Let me start by saying that the two terms are often confused. Indeed, some people in the industry use them virtually interchangeably, which is unfortunate, because they do reflect a valuable hierarchical difference. The Data Warehouse A "data warehouse" will typically contain the full range of business intelligence available to a company from all sources. That data consists of transaction-processing records, corporate and marketing data, and other business operations information; for example, a bank might include loans, credit card statements, and demand deposits data, along with basic customer information. This internal data is frequently combined with statistica...