AWT - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

AWT Events

AWT Events

shape Introduction

The present chapter explains about AWT events. The following topics are covered in AWT Events chapter:
  • Event Classes
  • Event Listeners
  • Event Handling
  • Event Adapters

Event

shape Description

An object change from one state to another state when used in an application is known as event. Events occurs when the user communicates with the Graphical User Interface. All the events are listened by the event listeners. There are two types of AWT Events, they are:
  • Background Events: The events that does not require the user action are known as Background Events. Software interruptions, and operating system failure are some background events.
  • Foreground Events : The events that require the user interaction are known as Foreground Events. Mouse click, and entering a character from the keyboard are some examples.

Event Classes

shape Description

Event Classes determines an event. It is the base class for all the AWT Events from which event objects are derived. java.util package is required to use these classes.

shape Declaration

The declaration of an event object can be given as follows: public class EventObject extends Object implements Serializable An event initially generates an object called protected Object source.

Event Listeners

shape Description

When an event occurs, a object is generated and a notification goes to it to listen the AWT Events. Event Object class is the super class of event listener object classes.

shape Description

java.util.EventListener can be declared as follows: public interface EventListener
EventClass Source Listener Interface Description
ActionEvent Button,List,MenuItem,Textfield ActionListener Created when a button is pressed, menu-item gets selected when list-item is double clicked
MouseEvent Mouse related event MouseListener Created when the mouse is clicked, dragged, or released
FocusEvent Component FocusListener Created when a component lose or gain keyboard focus.
ItemEvent CheckBox,CheckboxMenuItem,Choice,List ItemListener Created when list-item or checkbox is pressed.
KeyEvent Input from keyboard KeyListener Created when a input is given from the keyboard.
TextEvent Text Component TextListener Created when the textarea or textfield value is changed.
WindowEvent Window WindowListener Created when window is opened, closed, activated or iconified.
ComponentEvent Component ComponentEventListener Created when a component is moved, set visible, or hidden.
ContainerEvent Component ContainerListener Created when a component is added or removed to/from the container.
AdjustmentEvent Adjustable AdjustmentListener Created when the scroll bar is accessed.

Event Handling

shape Description

The standard process of giving directions when an event occurs is called as Event Handling. There are three main components in Event Handling, they are:
  1. Events
  2. Event Source
  3. Listeners.

shape Conceptual figure

Event Adapters

shape Description

Listener interfaces have abstract classes which gives null implementation of all the methods in that interface. Without directly implementing the listener interface, one can extend the adapter class and override the required methods. These methods will be empty.
Adapter Class Listener Interface
WindowAdapter WindowListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener

shape Examples

[javascript]import java.awt.*; import java.awt.event.*; import java.applet.*; public class UsingFonts extends Applet implements KeyListener { String msg=""; public void init() { addKeyListener(this); } public void keyPressed(KeyEvent k) { showStatus("KeyPressed"); } public void keyReleased(KeyEvent k) { showStatus("KeyRealesed"); } public void keyTyped(KeyEvent k) { msg = msg+k.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg, 80, 80); } }[/javascript] HTML Code [html] <html> <body> < applet code="UsingFonts" width=300, height=100 > </body> </html> [/html] Output Another example using MouseListener [javascript]import java.awt.Frame; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /* * To create a stand alone window, class should be extended from * Frame and not from Applet class. */ public class Splesson extends Frame implements MouseListener{ int x=0, y=0; String strEvent = ""; Splesson(String title){ //call superclass constructor with window title super(title); //add window listener addWindowListener(new MyWindowAdapter(this)); //add mouse listener addMouseListener(this); //set window size setSize(300,300); //show the window setVisible(true); } public void mouseClicked(MouseEvent e) { strEvent = "MouseClicked"; x = e.getX(); y = getY(); repaint(); } public void mousePressed(MouseEvent e) { strEvent = "MousePressed"; x = e.getX(); y = getY(); repaint(); } public void mouseReleased(MouseEvent e) { strEvent = "MouseReleased"; x = e.getX(); y = getY(); repaint(); } public void mouseEntered(MouseEvent e) { strEvent = "MouseEntered"; x = e.getX(); y = getY(); repaint(); } public void mouseExited(MouseEvent e) { strEvent = "MouseExited"; x = e.getX(); y = getY(); repaint(); } public void paint(Graphics g){ g.drawString(strEvent + " at " + x + "," + y, 50,50); } public static void main(String[] args) { Splesson myWindow = new Splesson("Window With Mouse Events Example"); } } class MyWindowAdapter extends WindowAdapter{ Splesson myWindow = null; MyWindowAdapter(Splesson myWindow){ this.myWindow = myWindow; } //closing the window public void windowClosing(WindowEvent we){ myWindow.setVisible(false); } }[/javascript] Output

Summary

shape Key Points

  • AWT Events refers to the process of object changing from one state to another./li>
  • Event Class is the base class for all events.
  • Event Listeners listen to the events.
  • Handling events has components: Events, Source and Listeners.
  • Adapter classes have abstract methods.

shape Programming Tips

windowClosing() method cleans up the code when the program is closed. To avoid that one can use the ToDoList() method, which backups the code.