Saturday, December 11, 2021

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

Checkbox class is used to create a checkbox control, which contains a box that can be checked or unchecked by clicking on it.

public class Checkbox extends Component implements ItemSelectable, Accessible  

Checkbox Class Constructors:

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.

Checkbox Class Methods:

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.

Ex:

import java.awt.*;   

import java.awt.event.*;   

public class CheckboxExample2 

{   

// constructor to initialize     

CheckboxExample2() {   

// creating the frame 

        Frame f = new Frame ("CheckBox Example");   

// creating the label 

        final Label label = new Label();           

// setting the alignment, size of label        

label.setAlignment(Label.CENTER);         

label.setSize(400,100);   

// creating the checkboxes      

Checkbox checkbox1 = new Checkbox("C++");        

checkbox1.setBounds(100, 100,  50, 50);        

Checkbox checkbox2 = new Checkbox("Java");      

checkbox2.setBounds(100, 150,  50, 50);   

// adding the checkbox to frame 

f.add(checkbox1); 

f.add(checkbox2);  

f.add(label);   

 // adding event to the checkboxes       

checkbox1.addItemListener(new ItemListener() {             

public void itemStateChanged(ItemEvent e) {                             

label.setText("C++ Checkbox: " + (e.getStateChange()==1?"checked":"unchecked"));               

}          

});         

checkbox2.addItemListener(new ItemListener() {               

public void itemStateChanged(ItemEvent e) {                              

label.setText("Java Checkbox: " + (e.getStateChange()==1?"checked":"unchecked"));             

}  

});   

// setting size, layout and visibility of frame      

f.setSize(400,400);         

f.setLayout(null);          

f.setVisible(true);   

  }   

// main method 

public static void main(String args[])   

{   

    new CheckboxExample2();   

}   

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top