1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* 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
Add Comment