Java AWT Checkbox

Java AWT Checkbox

The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from “on” to “off” or from “off” to “on”.

AWT Checkbox Class Declaration

  1. public class Checkbox extends Component implements ItemSelectable, Accessible

Java AWT Checkbox Example

  1. import java.awt.*;
  2. public class CheckboxExample
  3. {
  4.      CheckboxExample(){
  5.        Frame f= new Frame(“Checkbox Example”);
  6.         Checkbox checkbox1 = new Checkbox(“C++”);
  7.         checkbox1.setBounds(100,10050,50);
  8.         Checkbox checkbox2 = new Checkbox(“Java”true);
  9.         checkbox2.setBounds(100,15050,50);
  10.         f.add(checkbox1);
  11.         f.add(checkbox2);
  12.         f.setSize(400,400);
  13.         f.setLayout(null);
  14.         f.setVisible(true);
  15.      }
  16. public static void main(String args[])
  17. {
  18.     new CheckboxExample();
  19. }
  20. }

Output:

java awt checkbox example 1


 

Java AWT Checkbox Example with ItemListener

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class CheckboxExample
  4. {
  5.      CheckboxExample(){
  6.         Frame f= new Frame(“CheckBox Example”);
  7.         final Label label = new Label();
  8.         label.setAlignment(Label.CENTER);
  9.         label.setSize(400,100);
  10.         Checkbox checkbox1 = new Checkbox(“C++”);
  11.         checkbox1.setBounds(100,10050,50);
  12.         Checkbox checkbox2 = new Checkbox(“Java”);
  13.         checkbox2.setBounds(100,15050,50);
  14.         f.add(checkbox1); f.add(checkbox2); f.add(label);
  15.         checkbox1.addItemListener(new ItemListener() {
  16.              public void itemStateChanged(ItemEvent e) {
  17.                 label.setText(“C++ Checkbox: “
  18.                 + (e.getStateChange()==1?“checked”:“unchecked”));
  19.              }
  20.           });
  21.         checkbox2.addItemListener(new ItemListener() {
  22.              public void itemStateChanged(ItemEvent e) {
  23.                 label.setText(“Java Checkbox: “
  24.                 + (e.getStateChange()==1?“checked”:“unchecked”));
  25.              }
  26.           });
  27.         f.setSize(400,400);
  28.         f.setLayout(null);
  29.         f.setVisible(true);
  30.      }
  31. public static void main(String args[])
  32. {
  33.     new CheckboxExample();
  34. }
  35. }

Output:

java awt checkbox example 2

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java AWT Checkbox The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from “on” to “off” or from “off” to “on”. AWT Checkbox Class Declaration public class Checkbox extends Component implements ItemSelectable, Accessible Java AWT Checkbox Example import java.awt.*; public class CheckboxExample {      CheckboxExample(){        Frame f= new Frame(“Checkbox Example”);         Checkbox checkbox1 = new Checkbox(“C++”); […]

Tags:

....
...