ItemListener in Java Swing

ItemEvent represents the event which is generated when a menu item is selected or deselected, checkbox item is clicked or a list item is selected.

 

The method itemStateChanged() defined by the ItemListener interface is invoked when the state of an item is changed. The object of ItemEvent class is passed as method parameter.

The general form of the method defined in ItemListener interface is

Void itemStateChanged(ItemEvent itmevt)

Example....

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

/*

<APPLET

   CODE=checks2.class

   WIDTH=200

   HEIGHT=200 >

</APPLET>

*/

public class checks2 extends Applet implements ItemListener {

   Checkbox checkboxx1, checkboxx2, checkboxx3, checkboxx4;

   TextField text1;

   public void init()

   {

       checkboxx1 = new Checkbox("1");

       add(checkboxx1);

       checkboxx1.addItemListener(this);

       checkboxx2 = new Checkbox("2");

       add(checkboxx2);

       checkboxx2.addItemListener(this);

       checkboxx3 = new Checkbox("3");

       add(checkboxx3);

       checkboxx3.addItemListener(this);

       checkboxx4 = new Checkbox("4");

       add(checkboxx4);

       checkboxx4.addItemListener(this);

       text1 = new TextField(20);

       add(text1);

   }

   public void itemStateChanged(ItemEvent itmevt)

   {

       text1.setText("Check box " + ((Checkbox) itmevt.getItemSelectable()).getLabel() + " clicked!");

   }

}

ItemEvent in Java Swing

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @