JAVA EVENT
HANDLING
Java Event Handling
■ Event Handling is the change in the state of object or source.
■ Example:
– Event Handling is the mechanism that controls the event and decide what should
happen if an event occurs.
Delegation event model
■ Delegation Event Model Benefits of DEM model
- User interface logic is completely different
From event handler logic
■ Source Listener
is an object Event Handler
on which event generate response to event
occur
Event register
Click here
Java code
State change
Event Handler example
■ ActionListener Example
It is an interface
Used to handle events caused by source like button, menu item etc
Override the actionPerformed(ActionEvent e)
ItemListener Example
■ Used for radioButton, list, choice and checkbox AWT control
■ Override the public void itemStateChanged(ItemEvent ie)
■ Example
CheckBox c1 = new CheckBox(“sub”,null,false);
C1.addItemListener()
Public void itemStateChaged(ItemEvent ie){
if(c1.isSelected()){
}
}
KeyListener
■ Handles the events generated from keys of Keybord
■ Public void keyPressed(KeyEvent ke);
■ Public void keyTyped(KeyEvent ke);
■ Public void keyReleased(KeyEvent ke);
MouseMotionListener
■ Handles Event generated from Mouse
- Override the
public void mouseMoved(MouseEvent me)
Public void mouseDraged(MouseEvent me)
MouseListiner
■ Public void mousePressed(MouseEvent me)
■ Public void mouseClicked(MouseEvent me)
■ Public void mouseReleased(MouseEvent me)
■ Public void mouseEntered(MouseEvent me)
■ Public void mouseExited(MouseEvent me)