Monday, May 7, 2007

How can a GUI component handle its own events?

A GUI component handles its events by implementing the required event listener interface.It adds its own event listener in order to keep a track of all events associated with it.AWT has Java1.0 and Java1.1 of Event handling in different manners:
In Java1.0:Event handling is based on inheritance.A program catches and processes GUI events by subclassing GUI components and override either action() or handleEvent() methods.There are two possibilities in this scenario:

a)Each component is subclassed to specifically handle its target events. The results in too many classes.
b)All events or a subset for an entire hierarchy for handling a particular container; results in container's overridden action() or handleEvent() method and complex conditional statement for events processing.

The event handling in Java 1.0 had issues like cumbersome to handle by developers,flitering of events was not too efficient as it was done in a single method handleEvent().

In Java 1.1 these issues are resolved through delegation based event model.The GUI code can be seprated from event handling code which is cleaner,flexible,easier to maintanin and robust.In delegation event model,java.util.EventObject is the root class for event handling.An event is propagated from "Source" object(responsible for firing the event) to "Listener" object by invoking a method on the listener and passing in the instance of the event subclass which defines the event type generated.

A listener is commonly an "adapter" object which implements the appropriate listener/(s) for an application to handl of events. The listener object could also be another AWT component which implements one or more listener interfaces for the purpose of hooking GUI objects up to each other.

There can be following types of events:
A low-level event represents a low-level input or window-system occurrence on a visual component on the screen.
The semantic events are defined at a higher-level to encapsulate the semantics of a UI component's model.

The low event class hierarchy:

-java.util.EventObject

-java.awt.AWTEvent

-java.awt.event.ComponentEvent (component resized, moved, etc.)

-java.awt.event.FocusEvent (component got focus, lost focus)

-java.awt.event.InputEvent


-java.awt.event.KeyEvent (component got key-press, key-release, etc.)

-java.awt.event.MouseEvent (component got mouse-down, mouse-move, etc.)

-java.awt.event.ContainerEvent

-java.awt.event.WindowEvent

The semantics event class hierarchy:

-java.util.EventObject

-java.awt.AWTEvent

-java.awt.event.ActionEvent ("do a command")

-java.awt.event.AdjustmentEvent ("value was adjusted")

-java.awt.event.ItemEvent ("item state has changed")

-java.awt.event.TextEvent ("the value of the text object changed")

The low-level listener interfaces in AWT are as follows:

java.util.EventListener

- java.awt.event.ComponentListener

- java.awt.event.ContainerListener

- java.awt.event.FocusListener

- java.awt.event.KeyListener

- java.awt.event.MouseListener

- java.awt.event.MouseMotionListener

- java.awt.event.WindowListener

The semantic listener interfaces in AWT are as follows:

java.util.EventListener

-java.awt.event.ActionListener

-java.awt.event.AdjustmentListener

-java.awt.event.ItemListener

-java.awt.event.TextListener

There are following Adapter classes in AWT :

java.awt.event.ComponentAdapter

java.awt.event.ContainerAdapter

java.awt.event.FocusAdapter

java.awt.event.KeyAdapter

java.awt.event.MouseAdapter

java.awt.event.MouseMotionAdapter

java.awt.event.WindowAdapter

0 comments :

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker