Search This Blog

ADVANCED JAVA

C-PROGRAMMING

Wednesday, December 28, 2022

Java AWT Checkbox

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  

Checkbox Class Constructors

Sr. no.ConstructorDescription
1.Checkbox()It constructs a checkbox with no string as the label.
2.Checkbox(String label)It constructs a checkbox with the given label.
3.Checkbox(String label, boolean state)It constructs a checkbox with the given label and sets the given state.
4.Checkbox(String label, boolean state, CheckboxGroup group)It constructs a checkbox with the given label, set the given state in the specified checkbox group.
5.Checkbox(String label, CheckboxGroup group, boolean state)It constructs a checkbox with the given label, in the given checkbox group and set to the specified state.

Method inherited by Checkbox

The methods of Checkbox class are inherited by following classes:

  • java.awt.Component
  • java.lang.Object

Checkbox Class Methods

Sr. no.Method nameDescription
1.void addItemListener(ItemListener IL)It adds the given item listener to get the item events from the checkbox.
2.AccessibleContext getAccessibleContext()It fetches the accessible context of checkbox.
3.void addNotify()It creates the peer of checkbox.
4.CheckboxGroup getCheckboxGroup()It determines the group of checkbox.
5.ItemListener[] getItemListeners()It returns an array of the item listeners registered on checkbox.
6.String getLabel()It fetched the label of checkbox.
7.T[] getListeners(Class listenerType)It returns an array of all the objects registered as FooListeners.
8.Object[] getSelectedObjects()It returns an array (size 1) containing checkbox label and returns null if checkbox is not selected.
9.boolean getState()It returns true if the checkbox is on, else returns off.
10.protected String paramString()It returns a string representing the state of checkbox.
11.protected void processEvent(AWTEvent e)It processes the event on checkbox.
12.protected void processItemEvent(ItemEvent e)It process the item events occurring in the checkbox by dispatching them to registered ItemListener object.
13.void removeItemListener(ItemListener l)It removes the specified item listener so that the item listener doesn't receive item events from the checkbox anymore.
14.void setCheckboxGroup(CheckboxGroup g)It sets the checkbox's group to the given checkbox.
15.void setLabel(String label)It sets the checkbox's label to the string argument.
16.void setState(boolean state)It sets the state of checkbox to the specified state.

Java AWT Checkbox Example

In the following example we are creating two checkboxes using the Checkbox(String label) constructo and adding them into the Frame using add() method.

CheckboxExample1.java

  1. // importing AWT class  
  2. import java.awt.*;    
  3. public class CheckboxExample1  
  4. {    
  5. // constructor to initialize  
  6.      CheckboxExample1() {    
  7. // creating the frame with the title  
  8.        Frame f = new Frame("Checkbox Example");    
  9. // creating the checkboxes  
  10.         Checkbox checkbox1 = new Checkbox("C++");    
  11.         checkbox1.setBounds(100100,  5050);    
  12.         Checkbox checkbox2 = new Checkbox("Java"true);    
  13. // setting location of checkbox in frame          
  14. checkbox2.setBounds(100150,  5050);    
  15. // adding checkboxes to frame  
  16.         f.add(checkbox1);    
  17.         f.add(checkbox2);    
  18.   
  19. // setting size, layout and visibility of frame  
  20.         f.setSize(400,400);    
  21.         f.setLayout(null);    
  22.         f.setVisible(true);    
  23.      }    
  24. // main method  
  25. public static void main (String args[])    
  26. {    
  27.     new CheckboxExample1();    
  28. }    
  29. }    

Output:

java awt checkbox example 1

Java AWT Checkbox Example with ItemListener

In the following example, we have created two checkboxes and adding them into the Frame. Here, we are adding the ItemListener with the checkbox which displays the state of the checkbox (whether it is checked or unchecked) using the getStateChange() method.

CheckboxExample2.java

  1. // importing necessary packages  
  2. import java.awt.*;    
  3. import java.awt.event.*;    
  4. public class CheckboxExample2  
  5. {    
  6. // constructor to initialize   
  7.      CheckboxExample2() {    
  8. // creating the frame  
  9.         Frame f = new Frame ("CheckBox Example");    
  10. // creating the label  
  11.         final Label label = new Label();            
  12. // setting the alignment, size of label       
  13.    label.setAlignment(Label.CENTER);    
  14.         label.setSize(400,100);    
  15. // creating the checkboxes  
  16.         Checkbox checkbox1 = new Checkbox("C++");    
  17.         checkbox1.setBounds(100100,  5050);    
  18.         Checkbox checkbox2 = new Checkbox("Java");    
  19.         checkbox2.setBounds(100150,  5050);    
  20. // adding the checkbox to frame  
  21. f.add(checkbox1);  
  22. f.add(checkbox2);   
  23. f.add(label);    
  24.   
  25. // adding event to the checkboxes  
  26.         checkbox1.addItemListener(new ItemListener() {    
  27.              public void itemStateChanged(ItemEvent e) {                 
  28.                 label.setText("C++ Checkbox: "     
  29.                 + (e.getStateChange()==1?"checked":"unchecked"));    
  30.              }    
  31.           });    
  32.         checkbox2.addItemListener(new ItemListener() {    
  33.              public void itemStateChanged(ItemEvent e) {                 
  34.                 label.setText("Java Checkbox: "     
  35.                 + (e.getStateChange()==1?"checked":"unchecked"));    
  36.              }    
  37.           });    
  38. // setting size, layout and visibility of frame  
  39.         f.setSize(400,400);    
  40.         f.setLayout(null);    
  41.         f.setVisible(true);    
  42.      }    
  43. // main method  
  44. public static void main(String args[])    
  45. {    
  46.     new CheckboxExample2();    
  47. }    
  48. }    

Output:

java awt checkbox example 2


0 comments:

Post a Comment

OS

Top