R4R
Right Place For Right Person TM
 

R4R JAVAPHP ExamplesHandling Checkbox in java

previous

Home

Next

Handling Checkbox in java

In this example we are going to Handle Checkbox.
To Handle the Checkbox we are using three type of package. One is java.awt.* second is  java.applet.* and third is java.awt.event.*; .The java.awt.* is used for GUI interface. AWT classes are contain in this package. The  java.applet.* package is used to create an applet and java.awt.event.*; defined within the java.awt.*; package is a subclass of EventObject. It is a super class of all AWT-based event..

Checkbox support these constructors:

Checkbox():
Is use to creates a Checkbox Whose label is initially blank. The state is unchecked.
Checkbox(String str) :
Is use to create a Checkbox that contains String str as label. The state is unchecked.
Checkbox(String str, Boolean on):
It allows you to set the initial state of check box. if on is true then state is checked.

In our example first we implement IctionListener() each listener implement the IctionLinstner.
When an event is occur itemStateChange() interface is called. An ItemEventobject is supplied as the argument to this method
In our example we creates four Checkbox , The initial state of the first box is checked. the status of the check box is displayed. Each time you Change the state of a Checkbox, the status displayed is updated.



//creat Checkbox
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckBox" width=300 height=200>
</applet>
*/
public class CheckBox extends Applet implements ItemListener {
String msg="";
Checkbox one,two,three,four;
public void init(){
 one = new Checkbox("Apple");
 two = new Checkbox("Banana");
 three = new Checkbox("orange");
 four = new Checkbox("Grapes");
add(one);
add(two);
add(three);
add(four);
one.addItemListener(this);
two.addItemListener(this);
three.addItemListener(this);
four.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie){
repaint();
}
public void paint(Graphics g){
msg="Which is your fevorite fruite?";
g.drawString(msg,4,80);
msg="Apple:" + one.getState();
g.drawString(msg,4,100);
msg="Banana:" + two.getState();
g.drawString(msg,4,120);
msg="Orange:" + three.getState();
g.drawString(msg,4,140);
msg="Grapes:" + four.getState();
g.drawString(msg,4,160);
}
}

Download Source Code

Output Of Example

previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R