Java Program to implement Choice, Checkbox, radio button With event handling

Write a Java Program to implement Choice, Checkbox, radio button With event handling:-

public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ItemListener
{
Checkbox c1, c2, c3, c4, c5, c6;
CheckboxGroup cbg1, cbg2;
Choice cbo;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(500,400);
addControls();
setVisible(true);
}
private void addControls()
{
setLayout(new FlowLayout());
cbg1 = new CheckboxGroup();
cbg2 = new CheckboxGroup();
c1 = new Checkbox("C", true);
c2 = new Checkbox("C++",cbg2,true);
c3 = new Checkbox("Java",cbg2, true);
c4 = new Checkbox("Prolog", cbg1,false);
c5 = new Checkbox("Lisp", true, cbg1);
c6 = new Checkbox("Fortran");
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
c5.addItemListener(this);
c6.addItemListener(this);
//c2.setState(true);
//c5.setState(false);
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
add(c6);
cbo = new Choice();
cbo.add("Delhi");
cbo.add("Ajmer");
cbo.add("Jaipur");
cbo.add("Mumbai");
cbo.insert("Beawar",0);
cbo.add("Chandigarh");
cbo.add("Jalandhar");
cbo.add("Nasirabad");
cbo.add("Bharatpur");
cbo.select(3);
cbo.addItemListener(this);
add(cbo);
}
public void itemStateChanged(ItemEvent ie)
{
if (ie.getSource() instanceof Checkbox)
{
Checkbox c = (Checkbox) ie.getSource();
System.out.println(c.getLabel() + " : " + c.getState());
}
else if (ie.getSource() instanceof Choice)
{
System.out.println("Selected Index : " + cbo.getSelectedIndex());
System.out.println("Selected Item : " + cbo.getSelectedItem());
}
}
}
import java.awt.event.*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

Related Posts