ADVERTISEMENT
ADVERTISEMENT

Home » Java programming language

Java AWT Checkbox

Java | AWT Checkbox: In this tutorial, we will look at one of the Java AWT components, the AWT Checkbox with example.
Submitted by Saranjay Kumar, on April 29, 2020

The checkbox is a GUI component that is used to create a checkbox control. It contains a box that can be checked or unchecked by clicking on it. It is used to turn the state from "on" or "off" or from "off" to "on". It extends the Component class and can be added to containers like Frame and Panels.

Whenever a checkbox is clicked, it generates an event as its state changes. We can handle such events using the ItemListener interface. Any class implementing the ItemListener interface can handle events generated by a checkbox. On being clicked, an ItemEvent is fired and the corresponding ItemListener called. We will study the details of handling events in later sections.

Consider the following code -

import java.awt.*;
import javax.swing.*;

public class CreateCheckBox{

     CreateCheckBox()
     {
         Frame f = new Frame();
         Checkbox c1 = new Checkbox();
         Checkbox c2 = new Checkbox("C++");
         Checkbox c3 = new Checkbox("Java", true);
         
         f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
         f.setVisible(true);
         f.setSize(300,300);
         
         if(c3.getState() == true)
         c1.setLabel("Label Changed");
         
         
         f.add(c1);
         f.add(c2);
         f.add(c3);
     
     }
                  
        public static void main(String []args){
        CreateCheckBox ob = new CreateCheckBox();
        
     }
}

Output

Java AWT Checkbox

As can be seen in the code, we created 3 instances of Checkbox class. C1 is initialized without any label. C2 has a label with the same name that is passed as an argument at the time of object initialization. By default, this checkbox is unchecked. If we want the checkbox to be "checked" by default, we can create an object the same way as in c3. The second parameter is a boolean value which decides the state of the checkbox.

We can retrieve the current state of a checkbox using the getState() method. It returns a boolean value – false indicating that the box in unchecked true indicating that the box is checked.

Not only the user has the capability of changing the state of the checkbox, but even the programmer can change the state using the setState() function. For eg., if we call c1.setState(true) it would "check" the checkbox of c1 object.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.