ItemEvent

Advertisements

Prev Tutorial Next Tutorial

Event Handling for RadioButton, List, Choice

For handling event for RadioButton, List, Choice, checkbox you need to use ItemEvent class and ItemListener interface.

ItemEvent class and ItemListener interface are associalted with radio button, checkbox list, choice. This logic can be written in itemStateChanged() whenever we want to perform operation while changed the option in those component.

GUI ComponentEvent classListener InterfaceMethod (abstract method)
RadioButton, Checkbox, List, ChoiceItemEventItemListenerpublic void itemStateChanged(ItemEvent e)

Example of ItemEvent

import java.awt.*;
import java.awt.event.*;

class A
{
Frame f;
Label  statusLabel;
A()
{
  f=new Frame();
  f.setSize(500,300);
  f.setLayout(new BorderLayout());
  Panel p=new Panel();
  p.setBackground(Color.cyan);

  statusLabel = new Label();        
  statusLabel.setAlignment(Label.CENTER);
  statusLabel.setSize(350,100);

  Checkbox chkJava = new Checkbox("Java");
  Checkbox chkHtml = new Checkbox("Html");
  Checkbox chkPHP = new Checkbox("PHP");
    		  
  p.add(chkJava);
  p.add(chkHtml);
  p.add(chkPHP);      
  f.add("North",p);
    
  chkJava.addItemListener(new CustomItemListener());
  chkHtml.addItemListener(new CustomItemListener());
  chkPHP.addItemListener(new CustomItemListener());

  f.add(statusLabel);
  f.setVisible(true);

}

 class CustomItemListener implements ItemListener {
     public void itemStateChanged(ItemEvent e) {
        statusLabel.setText(e.getItem()
         +" Checkbox: " 
         + (e.getStateChange()==1?"checked":"unchecked"));
      }    
   }
}
class ItemEventEx
{

	public static void main(String[] args) 
	{
		A obj=new A();
	}
}

Download code Click

itemevent example
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements