Item Events in Java

In this section, you will learn about handling item events in java.

Item Events in Java



In this section, you will learn about handling item events in java. This demonstrates that the event generated when you select an item from the group of the items.

This program demonstrates how the item events is handled. You can perform several operations according to the selecting of  items from the item group.

Here, you will see the handling item event through the given program in which, a combo box and a text area have been taken. Items of the combo box are as follows : Red, Green, Blue. If you select an item from the combo box then the message with the item name will be displayed in the text area.

Choice() :
This is the constructor of the Choice class which creates combo box.

ItemEvent :
This is the ItemEvent class which indicates  an event on selecting or deselecting items from the item group. This event is passed by ItemListener object. The generated event is passed to all ItemListener objects which is registered to receive such types of event. This is done using the addItemListener() method of the object.

getItem() :
This is the method of the ItemEvent class which returns the value of the selected or deselected item.

Here is  the code of program :

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

public class AwtItemEvent extends Frame{
  TextArea txtArea;
  public AwtItemEvent(String title){
  txtArea = new TextArea();
  add(txtArea, BorderLayout.CENTER);
  Choice choice = new Choice();
  choice.addItemListener(new ItemListener(){
  public void itemStateChanged(ItemEvent e){
  txtArea.setText("This is the " + e.getItem() " color.\n");
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  add(choice, BorderLayout.NORTH);
  public static void main(String[] args){
  AwtItemEvent f = new AwtItemEvent("AWT Demo");

Download this example.

Share on Google+Share on Google+

Item Events in Java

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Item Events in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image