Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AWT CheckBox
2.1.
Syntax
3.
Checkbox Constructors
4.
Checkbox Methods
5.
Examples
5.1.
Code 1
5.2.
Code 2
6.
Frequently Asked Questions
6.1.
What is the use of the checkbox in AWT?
6.2.
How to group multiple checkboxes in AWT in Java?
6.3.
What is AWT classed in Java?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT CheckBox

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The checkbox control turns on (true) or off (false) an option. Each checkbox has a label that describes what the checkbox performs. A checkbox's status can be modified by clicking on it. In this article, we shall be exhaustively learning about AWT CheckBox and several examples to understand it more effortlessly.

Also Read About, Multithreading in java

AWT CheckBox

To make a checkbox, use the Checkbox class. It's used to turn a feature on (true) or off (false). A Checkbox's status changes as you click it, from "on" to "off" or "off" to "on."

Syntax

public class Checkbox extends Component implements ItemSelectable, Accessible
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Checkbox Constructors

Given below are the constructors used for creating an object of Checkbox Class.

Checkbox Methods

Let’s discuss some of the most commonly used methods in AWT Checkbox .

Examples

This section will look for a few codes related to AWT CheckBox.

Code 1

import java.awt.*;
public class CheckBoxExample {


CheckBoxExample() {    


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


Checkbox checkbox1 = new Checkbox("C++");    
checkbox1.setBounds(100, 100, 50, 50);    
Checkbox checkbox2 = new Checkbox("Java", true);    
        
checkbox2.setBounds(100, 150, 50, 50);    
 
f.add(checkbox1);    
f.add(checkbox2);    


f.setSize(400,400);    
f.setLayout(null);    
f.setVisible(true);    
}    
 
public static void main (String args[])    
{    
new CheckBoxExample();    
}    
}    

Output

In this example, we have created an AWT Checkbox with defined dimensions and sure checkboxes to check the working.;

Code 2

import java.awt.*;
import java.awt.event.ItemListener;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;


public class CheckBoxDemo {



JFrame jf;
Checkbox chk1, chk2, chk3, chk4;



CheckBoxDemo()
{
JFrame jf = new JFrame("In which language do you code?");


chk1 = new Checkbox(); 


chk2 = new Checkbox("Java",true);



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



jf.add(chk1);
jf.add(chk2);
jf.add(chk3);


jf.setLayout(new FlowLayout());
jf.setSize(300,120);
jf.setVisible(true);
}


public static void main(String[] ar)
{
new CheckBoxDemo();
}
}

Output

In this example, we have created an AWT Checkbox to ask the user to choose any one of them.

Practice it on online java compiler for better understanding.

Frequently Asked Questions

What is the use of the checkbox in AWT?

A Checkbox Class is included in AWT in Java. It's used when there's only one choice, true or false. When the checkbox is ticked, it is in the "on" (true) state; otherwise, it is in the "off" state (false).

How to group multiple checkboxes in AWT in Java?

It's used to organize a group of Checkboxes. Only one box may be checked at a time when checkboxes are combined. This example generates a checkbox group for grouping numerous checkboxes into a single unit. It is useful when choosing a single option from a list of multiples.

What is AWT classed in Java?

TextField, Label, TextArea, RadioButton, CheckBox, Choice, List, and other AWT API classes are available in java.awt package.

Conclusion

In this article, we have extensively discussed the AWT Checkbox. We have briefly introduced the topic along with its methods and constructors. We also have discussed a few codes with their output and solution, which explains the purpose of this blog.

If you want to explore more about AWT, visit here.

You can improve your skills in  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, Basics of Java and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you're just getting started to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you in enhancing your knowledge. 

"Happy Coding!".

Previous article
AWT TextArea
Next article
AWT Choice
Live masterclass