0% found this document useful (0 votes)
12 views6 pages

2023 09 08 AWT CheckBox

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

2023 09 08 AWT CheckBox

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

08/09/2013

CheckBox Class
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".

Constructor of CheckBox Class


Sr. no. Constructor Description
1. Checkbox() It constructs a checkbox with no string as
the label.
2. Checkbox(String label) It constructs a checkbox with the given
label.
3. Checkbox(String label, boolean It constructs a checkbox with the given
state) label and sets the given state.
4. Checkbox(String label, Boolean It constructs a checkbox with the given
state, CheckboxGroup group) label, set the given state in the specified
checkbox group.

Checkbox Class Methods


Sr. Method name Description
no.
1. void addItemListener(ItemListener IL) It adds the given item listener to get the
item events from the checkbox.
2. AccessibleContext It fetches the accessible context of
getAccessibleContext() checkbox.
3. void addNotify() It creates the peer of checkbox.
4. CheckboxGroup getCheckboxGroup() It determines the group of checkbox.
5. ItemListener[] getItemListeners() It returns an array of the item listeners
registered on checkbox.
6. String getLabel() It fetched the label of checkbox.
7. T[] getListeners(Class listenerType) It returns an array of all the objects
registered as FooListeners.
8. Object[] getSelectedObjects() It returns an array (size 1) containing
checkbox label and returns null if checkbox
is not selected.
9. boolean getState() It returns true if the checkbox is on, else
returns off.
10. protected String paramString() It returns a string representing the state of
checkbox.
11. protected void processEvent(AWTEvent It processes the event on checkbox.
e)
12. protected void It process the item events occurring in the
processItemEvent(ItemEvent e) checkbox by dispatching them to
registered ItemListener object.
13. void removeItemListener(ItemListener It removes the specified item listener so
l) that the item listener doesn't receive item
events from the checkbox anymore.
14. void It sets the checkbox's group to the given
setCheckboxGroup(CheckboxGroup g) checkbox.
15. void setLabel(String label) It sets the checkbox's label to the string
argument.
16. void setState(boolean state) It sets the state of checkbox to the
specified state.

C:\Users\HP\CheckBoxExample1.java
1 import java.awt.*;
2
3
4 public class CheckBoxExample1
5{
6
7 CheckBoxExample1()
8 {
9 Frame f = new Frame("Check Box Example 1 ");
10
11 Checkbox c1= new Checkbox("Advanced Java");
12 c1.setBounds(100,50,100,50);
13 f.add(c1);
14
15
16 Checkbox c2= new Checkbox("Python");
17 c2.setBounds(100,100,100,50);
18 f.add(c2);
19
20
21 f.setSize(500,500);
22 f.setLayout(null);
23 f.setVisible(true);
24
25 }
26
27
28 public static void main(String args[])
29 {
30 CheckBoxExample1 myobj= new CheckBoxExample1();
31 }
32 }
33

Output :
C:\Users\HP\CheckBoxwithItemListener.java
1 import java.awt.*;
2 import java.awt.event.*;
3
4
5 public class CheckBoxwithItemListener
6{
7 Label l1;
8 Label l2;
9
10
11 CheckBoxwithItemListener()
12 {
13
14
15
16 Frame f = new Frame("Check Box Example with Item Listener ");
17
18 l1= new Label();
19 l1.setBounds(100,200,100,50);
20 f.add(l1);
21
22 l2= new Label();
23 l2.setBounds(100,250,100,50);
24 f.add(l2);
25
26 l1.setSize(200,40);
27 l2.setSize(200,40);
28
29 Checkbox c1= new Checkbox("Advanced Java");
30 c1.setBounds(100,50,100,50);
31 f.add(c1);
32
33
34 Checkbox c2= new Checkbox("Python");
35 c2.setBounds(100,100,100,50);
36 f.add(c2);
37
38
39 f.setSize(500,500);
40 f.setLayout(null);
41 f.setVisible(true);
42
43 // Adding ItemListener to Checkbox 1
44
45 // listener_1 is object of ItemListener
46
47 ItemListener listener_1=new ItemListener(){
48 public void itemStateChanged(ItemEvent e)
49 {
50 // l.setText("Hello");
51 if (e.getStateChange()==1)
52 {
53 //System.out.println("Cheked");
54 l1.setText("AJP : " + " Checked ");
55 }
56 else
57 {
58 //System.out.println("UnCheked");
59 l1.setText("AJP : " + " UnChecked ");
60 }
61
62
63 }
64 };
65
66 // we are passing here object of ItemListener
67 c1.addItemListener(listener_1);
68
69
70
71
72 c2.addItemListener(new ItemListener(){
73 public void itemStateChanged(ItemEvent e)
74 {
75 //l.setText("How r u ???? ");
76 if (e.getStateChange()==1)
77 {
78 //System.out.println("Cheked");
79 l2.setText("PYTHON : " + " Checked ");
80 }
81 else
82 {
83 //System.out.println("UnCheked");
84 l2.setText("PYTHON : " + " UnChecked ");
85 }
86 }
87 });
88
89 }
90
91
92
93
94 public static void main(String args[])
95 {
96 CheckBoxwithItemListener myobj= new CheckBoxwithItemListener();
97 }
98 }
99

Output:

You might also like