Applet Radio Button with ItemListener



In this tutorial, it shows on how to create radio button and implement item listener in java applet.

Code snippet

import java.awt.*;
import java.awt.event.*;
public class RadioButton extends java.applet.Applet implements ItemListener{
   
     CheckboxGroup radioGroup;
     Checkbox radio1;
      Checkbox radio2;
     Checkbox radio3;
            Checkbox radio4;
            TextField txt;
    
  public void init(){
 
          setLayout(null);
          radioGroup = new CheckboxGroup();
          radio1 = new Checkbox("Apple", radioGroup,false);
          radio2 = new Checkbox("Banana ", radioGroup,false);
          radio3 = new Checkbox("PineApple", radioGroup,false);
          radio4 = new Checkbox("Guava", radioGroup,false);
          txt=new TextField(20);
          add(radio1);
          add(radio2);
          add(radio3);
          add(radio4);
          add(txt);
          radio1.setBounds(20,20,100,20);
          radio2.setBounds(20,50,100,20);
          radio3.setBounds(20,80,100,20);
          radio4.setBounds(20,110,100,20);
          txt.setBounds(130,110,100,20);
          radio1.addItemListener(this);
          radio2.addItemListener(this);
          radio3.addItemListener(this);
          radio4.addItemListener(this);
            }

    public void paint(Graphics g) {
       
    }
    public void itemStateChanged(ItemEvent e){
      ItemSelectable source=e.getItemSelectable();
     
      if (source==radio1){
            txt.setText("10");}
      if (source==radio2){
            txt.setText("5");}
      if (source==radio3){
            txt.setText("20");}
      if (source==radio4){
            txt.setText("15");}
    }
}