Sunday, 5 August 2018

Item events in java programming


Introduction

In this post ,you will learn item events handling in java programming.when you select an item from item group then event generated.This java program show how the item events is handled .you can do several operations according to the selecting of items from the item group.In this programs item event generated through the given options we use a in this program a combo box and a text area.In the combo box present following items : Red , Green, Blue.if you select an item from the combo box then the message with the item name will 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 :
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.*;
import java.awt.event.*;
public class NewClass extends Frame{
TextArea txtArea;
public NewClass(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){
NewClass f = new NewClass("AWT program");
}
}

0 comments:

Post a Comment