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.

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