Pageviews

Sunday 7 July 2013

Event handling in Java

by Unknown  |  in Java at  Sunday, July 07, 2013

Event handling is the heart of successful applet programming.Applets are event driven programs.Event is an object that describes a change of state.This change of state is generated by the user interacting with the Graphical User Interface(GUI) by pressing a button,entering a character via keyboard,dragging the mouse,selecting an item in Combo box,selecting an item in a list. Mostly,the events to which our applet will respond are generated by the user. These events are passed to our applets in a variety of ways. There are several types of events. The most commonly handled events are generated by the following:


  • Mouse
  • Keyboard 


and various controls, such as a push button,submit buttons or login buttons.Event handling in Java is supported by the java.awt.event package.

Event handling
Table 1:   Main Event classes in java.awt.event package


Event Listener Interfaces:

Event Listener receives notifications of events.Listeners are created by implementing one or more interfaces.Whenever an event occurs,the event source invokes the appropriate method defined by the listener and provides an event object as its argument.The commonly used listener interfaces are:

Event handling

Table 2: Interface classes for Event handling
Source code for creating a simple Form in Java

import java.awt.*;
import java.io.*;

class Form1 extends Frame
{
int i;
Label lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,lbl8,lbl9;
TextField  txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9;
GridLayout gl1;

Form1()
{
gl1=new GridLayout(12,2);
setLayout(gl1);
lbl1=new Label("First Name");
txt1=new TextField(8);
lbl2=new Label("Second Name");
txt2=new TextField(8);
lbl3=new Label("Last Name");
txt3=new TextField(8);
lbl4=new Label("Father's Name");
txt4=new TextField(8);
lbl5=new Label("Father's Occupation");
txt5=new TextField(8);
lbl6=new Label("Mother's Name");
txt6=new TextField(8);
lbl7=new Label("Mother's Occupation");
txt7=new TextField(8);
lbl8=new Label("LL.No");
txt8=new TextField(8);
lbl9=new Label("Mobile.No");
txt9=new TextField(8);

add(lbl1);
add(txt1);

add(lbl2);
add(txt2);

add(lbl3);
add(txt3);

add(lbl4);
add(txt4);

add(lbl5);
add(txt5);

add(lbl6);
add(txt6);

add(lbl7);
add(txt7);

add(lbl8);
add(txt8);

add(lbl9);
add(txt9);

}
public static void main(String arg[])
{
try{
Form1 obj=new Form1();
obj.setSize(500,400);
obj.setTitle("Personal details");
obj.setVisible(true);
}
catch(NullPointerException ae)
{
}
}
}

The output of the program is as below:

Event handling




We had used Text Editor to write our code,after this we will move onto IDE(Integrated Development Environment).We had used Text editors till now just to perfect our coding part,because in IDE most part of the code is written automatically by the IDE.













0 comments:

Proudly Powered by Blogger.