AWT Checkbox and Radio button in Java with Example

AWT Checkbox and Radio button in Java with Example

Table of Components

a) Introduction to Check Box and Radio Button
b) Constructors of Check Box
c) Example with Source Code
d) Output
************

Introduction

Checkbox and Radio button are the AWT Components. Checkbox is the component of the AWT which is used to create two types of components they are check box and radio button itself. Radio button means only one selection of item whereas check box means multiple selections of items. Checkbox generates ItemEvent.

There are three constructor of check box, they are;
Checkbox() – it is use to create the checkbox with no label.
Checkbox(String) – it is used to create a checkbox with label
Checkbox(String, CheckboxGroup, Boolean) – creates radio button.

Some useful methods used in check box are:
getLabel() – it is used to get the text associated with checkbox.
getState() – it returns ‘true’ or ‘false’ for selected or unselected items.

How to create a check box and radio button?

If you want to create the check box then you should use first two constructor i.e. Checkbox() and Checkbox(String). And if you want to create the radio button then you should compulsorily use third constructor i.e.
Now just be clear everything by seeing the source code and its output.

Source Code:

/*
A AWT GUI TO DEMONSTRATE THE CHECK BOX AND RADIO BUTTON.
 */
package AWTComponents;

/**
 *
 * @author AnkitPC
 */
import java.awt.*;
import java.awt.event.*;
// it is a awt frame so we need to extend the window adapter class to make it closable
public class CheckBoxandRadioButtonDemo extends WindowAdapter {
    // awt components
    Frame f;
    Checkbox c1,c2,c3,c4;
    Label l1,l2;
    // constructor
    CheckBoxandRadioButtonDemo(){
        f=new Frame("Checkbox and Radio Button Demo");
        // frame size
        f.setSize(250,300);
        // registering the event
        f.addWindowListener(this);
        // set layout
        f.setLayout(new FlowLayout());
       
        // helper class for creating the radio button
        CheckboxGroup cbg=new CheckboxGroup();
        // using third constructor to create radio button
        c1=new Checkbox("Male",cbg,true);
        c2=new Checkbox("Female",cbg,false);
        // using second constructor
        c3=new Checkbox("Music");
        c4=new Checkbox("Reading");
       
        l1=new Label("Gender: ");
        l2=new Label("Hobbies: ");
       
        // add components to the frame
        f.add(l1);
        f.add(c1);
        f.add(c2);
        f.add(l2);
        f.add(c3);
        f.add(c4);
       
        f.setVisible(true);
       
    }
   
    public static void main(String[] args) {    
        // call constructor
        new CheckBoxandRadioButtonDemo();
       
    }
    // performing action
    // to close the awt frame
    public void windowClosing(WindowEvent e){
        f.dispose();
      }
    }

Output:
The video output is attached to see you clear.



Post a Comment

0 Comments