Event Handling Concept Simplified in AWT in Java

Spread the love
  • 1

The components we create in general like button,radio,frame etc do not do anything on their own with respect to user input .To make a valid GUI ,we need to provide controls to these components. Providing controls to a component is done via GUI programming or Event Driven programming. Below are the events:

  • Windows Events-Destroying, exposing,iconifying,deconifying and moving windows.
  • Mouse Events-Moving the mouse,dragging a mouse,enter and exit from components,engage and release button of a mouse.
  • Keyboard Events-pressing a key and releasing a key.
  • List Events-Selecting and deselecting a list item.
  • Scrolling Events:-Scrolling a line up or down. page up or down or scrolling to an exact location.
For each type of component object in AWT, there is a handleEvent() method.When a device activity occurs that generates an event,the event is first sent to the component which in turn calls the event handler of the component class. The event handler there after returns a boolean value as either false or true depending on the event handling routine. if it returns false,the event is passed on the component’s parent(container).If the method returns true,it means the component acted on the event and more processing of the event takes place.
Various handling methods available with Event class and their practical usage.Event handler uses number of information regarding the activity that took place.These information variables are designed in Event class.When an event occurs, the corresponding event object is instructed and these variables are initialized.
The variables includes:

Object target;//the component that originally received the event.
long when;//the time stamp of when the event occured.
int id;//the type of the event
int x;//the x coordinate of the event.
int y;//the y coordinate of the event. 
int key;//the key that was passed in a keyboard event.
int modifier;//the state of the modifier key(Shift,control,alt)
int clickCount;//the number of consecutive clicks.
Object arg;//an event dependent argument that helps to identify the event causing GUI events.(Label of a button)

Different event types possible are listed below:

Event types Details Comments
These events occurs when the corresponding activity is performed on a window object.
Mouse Events MOUSE_ENTER
These are generated by mouse actions within the borders of canvas,Frame and Panel components
Focus Events GOT_FOCUS
Generated as the mouse enters or exits the borders of canvas,Frame and Panel components.They
are also generated as we click a mouse in a Text,TextArea,List,Combo etc components.
Keyboard Events KEY_PRESSED
Generated by pressing or releasing keys on the keyboard when we are on the borders of canvas,Frame
and Panel components or inside a Text,TextArea,List,Combo etc components.
Action Events ACTION_EVENT Generated when we click on a Button,choice or checkbox component,press enter in a TextFiled or double
click on a list item.
Scrollbar Event SRCOLL_LINE_UP
Generated by actions involving the scrollbar in scrollbar component.

The id variable in the Event class will be initiated by the corresponding event type during the automatic instantiation of the respective Event object. There are numbers of event handling methods declared in component class and Event class. The method handleEvent(Event evt) in component class is the central event dispatcher for a component.This method for a given event type(evt.id) calls the corresponding method.

evt.id handleEvent.class
Event MOUSE_DOWN mouseDown(Event e,int x,int y)
Event MOUSE_UP mouseUp(Event e,int x,int y)
Event MOUSE_DRAG mouseDrag(Event e,int x,int y)
Event MOUSE_ENTER mouseEnter(Event e,int x,int y)
Event MOUSE_EXIT mouseExit(Event e,int x,int y)
Event KEY_PRESS keyDown(Event e,int key)
Event KEY_ACTION keyDown(Event e,int key)
Event KEY_RELEASE keyUp(Event e,int key)
Event KEY_ACTION_RELEASE keyUp(Event e,int key)
Event LOST_FOCUS lostFocus(Event e,Object o)
Event GOT_FOCUS getFocus(Event e,Object o)
Event ACTION_EVENT action(Event e,Object o)

List and Scrollbar components do not have their helper functions.To handle these, it is required to override the handleEvent() method.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1


Please enter your comment!
Please enter your name here