Home Java Example Java Awt Item Events in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Item Events in Java

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

Item Events in Java

     

Introduction

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){
  super(title);
  txtArea = new TextArea();
  add(txtArea, BorderLayout.CENTER);
  Choice choice = new Choice();
  choice.addItem("red");
  choice.addItem("green");
  choice.addItem("blue");
  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){
  System.exit(0);
  }
  });
  add(choice, BorderLayout.NORTH);
  setSize(400,400);
  setVisible(true);
  setResizable(false);
  }
  
  public static void main(String[] args){
  AwtItemEvent f = new AwtItemEvent("AWT Demo");
  }
}

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Ask Questions?    Discuss: Item Events in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com