Java Event Handling

 

Event Handling

Event Driven Programming (DEM)

§  In GUI environment, action are initiated by the press of a button, key press etc.

§  Therefore appropriate mechanism are required to capture such event and to react to the events by executing piece of code.

§  Java provides such mechanism.

§  Events in java are handled by delegation event model.

§  In DEM there is source which generate the event, then listener listen the event and initiate an action. 

§  A listener has to register with the source.

§  Any number of listener can register with the source.

§  When any event take place with source, the registered listener notified, and then the listener initiate some actions.

 


Events

§  An event is an object that describes, change of state of source.

§  The super class of all AWT events is java.util.AWTEvent class

§  AWTEvent class is inherited from EventObject class

§  AWTEvent class is an abstract class.

§  It has many concrete subclasses.

§  getID() is important method defined in AWTEvent class. This method returns int that representing the type of event.

 

 

 

 



Classes of java.awt.event package
are subclass of AWTEvent class

§  ActionEvent class:

This class works with high level event. The event occurs when the component specific  action takes place.

§  KeyEvent class:

This class is useful when the events generated by key strokes.

 

§  ItemEvent class

The class work with events generated when checkbox or list item is clicked.

 

§  TextEvent class

This class work with event generated by the change of object’s text.

 

 

The ActionEvent class

§  This event is generated when button is pressed or menu item is selected.

§  This is high level(or semantic) event.

§  The constants of ActionEvent class are:

§  ALT_MASK

§  CTRL_MASK

§  META_MASK

§  SHIFT_MASK

Constructors of ActionEvent class

§  ActionEvent(Object src, int id, String cmd)

§  ActionEvent(Object src, int id, String cmd, int modifier)

 

Src- source object

Id- identify the event

Cmd- command associated with the event

Modifier- modifier key was pressed when the event was generated

Methods of ActionEvent class

§  String getActionCommand()

This method returns the command name for the invoking ActionEvent class.

§  Int getModifier()

Identifies which modifier key was pressed when event generated

§  String paramString()

Returns a string identifying the event.

 

 

 

ItemEvent class

ItemEvent is generated when checkbox is status is changed. ItemEvent is high level event.

 

Constants of ItemEvent class are:

§  DESELECTED

§  ITEM_STATE_CHANGED

§  SELECTED

Constructor of ItemEvent Class

  ItemEvent(ItemSelectable src, int id, Object item, int stateChange)

 

Src – item selectable object then generate the event

Id- it indicates the type of event

Item- it is an object that is affected by the event

stateChange- it suggested that the item is selected or deselected

 

Methods of ItemEvent class

§  ItemSelectable getItemSelectable()

it returns the ItemSelectable object that originated the event

§  Object getItem

Returns the item object that was affected by the event

§  Int getStateChange()

It identify that, item is selected or deselected

§  String paramString()

Returning a String identifying the event

 

 

KeyEvent class

§  The KeyEvent is generated when a key is pressed, typed or released.

 

§  The key typed event is generated only when a character is generated.

 

§  Java use virtual key code to represent which key on keyboard has been pressed rather then which character was generated.

Constants in KeyEvent class

§  KEY_TYPED

§  KEY_PRESSED

§  KEY_RELEASED

§  VK_0 to VK_9

§  VK_A to VK_Z

Other virtual key constants are:

§  VK_ENTER

§  VK_CANCEL

§  VK_CONTROL and many more virtual key constants are there.

 

Constructors of KeyEvent class

KeyEvent(Component src, int id,long when,int modifier, int keycode, char keychar)

 

KeyEvent(Component src, int id, long when, int modifier, int keyCode)

 

Src – component that originated the event

Id- identify the event type

When- specify the time

Modifier- modifier key down during the event

keyCode- integer code for an actual key

KeyChar – Unicode character generated by this event

 

Methods of KeyEvent class

§  int getKeyCode()

§  void setKeyCode()

§  void setKeyChar(char keychar)

§  char getKeyChar()

§  String getKeyText(int keyCode)

 

TextEvent class

A text event is generated when the text of an object is changed.

The TEXT_VALUE_CHANGED constant indicates the object text is changed.

§  Constructor:

TextEvent(Object src, int id)

Src- the text component object

Id- identifies event

 

TextEvent class method

 

String paramString()

    This method returns a String identifying this text event.

 

Event Listeners

§  When any event occurs they are notify the listeners, that are registered with the source.

§  An event listener has to initiate some action when the happening of an event is notified to it.

§  What action to be taken is depend on the programmer.

§  Many interfaces are designed for listeners with event as argument.