Any program that uses GUI (graphical user interface) such as Java application written for windows, is event driven. Event describes the change in state of any object. For Example: Pressing a button, entering a character in Textbox, Clicking or Dragging a mouse, etc.
Event handling has three main components,
- Events: An event is a change in state of an object.
- Events Source: Event source is an object that generates an event.
- Listeners: A listener is an object that listens to the A listener gets notified when an event occurs.
How Events are handled?
A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return. Events are supported by a number of Java packages, like java.util, java.awt and java.awt.event.
Important Event Classes and Interface
Event Classes |
Description |
Listener Interface |
ActionEvent |
generated when button is pressed, menu-item is selected, list-item is double clicked |
ActionListener |
MouseEvent |
generated when mouse is dragged, moved,clicked,pressed or released and also when it enters or exit a component |
MouseListener |
KeyEvent |
generated when input is received from keyboard |
KeyListener |
ItemEvent |
generated when check-box or list item is clicked |
ItemListener |
TextEvent |
generated when value of textarea or textfield is changed |
TextListener |
MouseWheelEvent |
generated when mouse wheel is moved |
MouseWheelListener |
WindowEvent |
generated when window is activated, deactivated, deiconified, iconified, opened or closed |
WindowListener |
ComponentEvent |
generated when component is hidden, moved, resized or set visible |
ComponentEventListener |
ContainerEvent |
generated when component is added or removed from container |
ContainerListener |
AdjustmentEvent |
generated when scroll bar is manipulated |
AdjustmentListener |
FocusEvent |
generated when component gains or loses keyboard focus |
FocusListener |
Example of ActionListener
JButton btn=new JButton(“Click Here”); btn.addActionListener(new ActionListener(){
pubic void actionPerformed(ActionEvent ae){
//event handing code here
}
});
Example of ItemListener
JComboBox cmb=new JComboBox(arr); cmb.addItemListener(new ItemListener(){
pubic void actionPerformed(ItemEvent ie){
if (ie.getStateChange() == ItemEvent.SELECTED)
//event handing code here
}
});
Keyboard and Mouse Events
The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods.
void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e);
Mouse Event Example
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener
{
Label l; MouseListenerExample(){
addMouseListener(this);
l=new Label(); l.setBounds(20,50,100,20);
add(l);
setSize(300,300); setLayout(null); setVisible(true);
}
public void mouseClicked(MouseEvent e) { l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) { l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) { l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) { l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) { l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. The KeyListener interface is found in java.awt.event package. It has three methods.
void keyPressed(KeyEvent e);
void keyReleased(KeyEvent e);
void keyTyped(KeyEvent e);
Key Event Example
import java.awt.*;
import java.awt.event.*;
public class KeyListenerExample extends Frame implements KeyListener{ Label l;
TextArea area; KeyListenerExample(){
l=new Label(); l.setBounds(20,50,100,20); area=new TextArea(); area.setBounds(20,80,300, 300); area.addKeyListener(this); add(l);add(area); setSize(400,400); setLayout(null); setVisible(true);
}
public void keyPressed(KeyEvent e) { l.setText("Key Pressed");
}
public void keyReleased(KeyEvent e) { l.setText("Key Released");
}
public void keyTyped(KeyEvent e) { l.setText("Key Typed");
}
public static void main(String[] args) {
new KeyListenerExample();
}
}