Get Checkbox State Example
- /*
- Get Checkbox State Example
- This java example shows how to get state of a Checkbox using
- getState method of AWT Checkbox class.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- import java.awt.Graphics;
- /*
- <applet code="GetCheckboxState" width=200 height=200>
- </applet>
- */
- public class GetCheckboxState extends Applet{
- Checkbox checkBox1 = null;
- Checkbox checkBox2 = null;
- public void init(){
- //create checkboxes
- checkBox1 = new Checkbox("My Checkbox 1");
- //create checked checkbox
- checkBox2 = new Checkbox("My Checkbox 2", true);
- //add checkboxes using add method
- add(checkBox1);
- add(checkBox2);
- }
- public void paint(Graphics g){
- /*
- * To get state of a checkbox use,
- * boolean getState()
- * method of AWT Checkbox class.
- *
- * It returns true if checkbox is checked or selected,
- * false otherwise.
- */
- g.drawString("Is checkbox 1 selected? " + checkBox1.getState(), 10, 80);
- g.drawString("Is checkbox 2 selected? " + checkBox2.getState(), 10, 100);
- }
- }
Example Output




