CheckBox in Java AWT


Checkbox control is used to turn an option on(true) or off(false). There is label for each checkbox representing what the checkbox does.

Constructor Used for
Checkbox() checkbox with no string as the label.
Checkbox(String label) checkbox with the given label.
Checkbox(String label, boolean state) checkbox with the given label and sets the given state.
Checkbox(String label, boolean state, CheckboxGroup group) checkbox with the given label, set the given state in the specified checkbox group.
Checkbox(String label, CheckboxGroup group, boolean state) checkbox with the given label, in the given checkbox group and set to the specified state.

This is a Java program that demonstrates how to create checkboxes in AWT. The program creates a window with a title "Tutor Joes" and a size of 1000x600 pixels. It sets the layout of the window to null, which means that the components added to the window will be manually positioned using absolute coordinates.

The program creates three checkboxes labeled "C Program", "C++ Program", and "Java Program" using the Checkbox class. It also creates three labels to display the state of each checkbox.

The program adds an ItemListener to each checkbox to detect changes in their state. When a checkbox is checked or unchecked, its corresponding label is updated to display its new state. The program then adds the checkboxes and labels to the window using the add() method.

The WindowAdapter class is used to handle the window closing event. When the user clicks the close button, the windowClosing() method is called, which terminates the program using the System.exit(0) method. The main() method creates an instance of the MyApp class and shows the window on the screen.

Source Code

package awtDemo;
 
import java.awt.*;
import java.awt.event.*;
//CheckBox in AWT
class MyApp  extends Frame{
 
	Label l1,l2,l3;
	Checkbox c1,c2,c3;
 
 
	public MyApp() {
		super("Tutor Joes");
		setSize(1000,600);//w,h
		setLayout(null);
		setVisible(true);
 
		c1=new Checkbox("C Program");
		c1.setBounds(10,50,250,30);
		l1=new Label("Not Selected");
		l1.setBounds(300,50,600,30);
 
 
		c2=new Checkbox("C++ Program");
		c2.setBounds(10,100,250,30);
		l2=new Label("Not Selected");
		l2.setBounds(300,100,600,30);
 
		c3=new Checkbox("Java Program");
		c3.setBounds(10,150,250,30);
		l3=new Label("Not Selected");
		l3.setBounds(300,150,600,30);
 
		c1.addItemListener(new ItemListener() {
	         public void itemStateChanged(ItemEvent e) {             
	             l1.setText((e.getStateChange()==1?"checked":"unchecked"));
	          }
	       });
		c2.addItemListener(new ItemListener() {
	         public void itemStateChanged(ItemEvent e) {             
	             l2.setText((e.getStateChange()==1?"checked":"unchecked"));
	          }
	       });
		c3.addItemListener(new ItemListener() {
	         public void itemStateChanged(ItemEvent e) {             
	             l3.setText((e.getStateChange()==1?"checked":"unchecked"));
	          }
	       });
 
		add(c1);
		add(l1);
 
		add(c2);
		add(l2);
 
		add(c3);
		add(l3);
 
		//Close Button Code
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}
}
 
 
public class app{
	public static void main(String[] args) {
		MyApp frm=new MyApp();
	}
 
}
 
To download raw file Click Here

Output

Java AWT


Basic Programs