Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java event handling

Event handling in Java refers to executing some code when specific actions occur such as a window being minimized or a button being clicked. There are special interfaces for this purpose and they are located in the java.awt.event package.

This lesson focuses on:

Handling window events

To handle window events, the WindowListener interface is used. The WindowListener interface has various methods to handle window events.

Methods of the WindowListener interface:

Handling action events

To handle action events (such as the clicking of a button), the ActionListener interface is used.

Methods of the ActionListener interface:

Handling focus events

To handle focus events (such as when an object such as a textbox is awaiting user input), the FocusListener interface is used.

Methods of the FocusListener interface:

Handling item events

Item events are triggered by components that allow for a selection of items such as Checkboxes, and Lists. To handle item events, the ItemListener interface is used.

Methods of the ItemListener interface:

Handling adjustment events

Adjustment events are triggered by components that are adjustable such as a scroll bar. To handle adjustment events, the AdjustmentListener interface is used.

Methods of the AdjustmentListener interface:

Handling text events

Text events are triggered by components whose fields are editable such as textboxes. To handle text events, the TextListener interface is used.

Methods of the TextListener interface:

Handling mouse events

Mouse events are triggered by the computers mouse. Mouse events are actually handled by two interfaces - MouseListener and MouseMotionListener.

The MouseListener interface is used to detect the clicking of mouse buttons or the moving of a mouse into or out of a listener's area. The MouseMotionListener interface is used to detect the movement of the mouse.

Methods of the MouseListener interface:

Methods of the MouseMotionListener interface:

Handling key events

Key events are triggered by keyboard actions. Key events are handled by the KeyListener interface.

Methods of the KeyListener interface:

Making objects listen for events

Once you implement the necessary interfaces, and define the implementation of their methods, you need to make objects listen for events. For example, if you implement the ActionListener interface, and you define what its actionPerformed() method does, and you want the method to be called every time a certain button is clicked, you need to make that button listen for the event.

To make windows listen for window events defined by the methods of the WindowListener interface, use the addWindowListener() method.

Example:

import java.awt.*;
import java.awt.event.*;

class FrameWithEvents implements WindowListener{

    //the constructor for 
    //the FrameWithEvents class
    public FrameWithEvents(){

    Frame aFrame = new Frame();
    aFrame.setSize(200, 200);
    aFrame.addWindowListener(this);
    aFrame.setVisible(true);        

    }

    public static void main(String[] args){
   
    FrameWithEvents FWE = new FrameWithEvents();

    }


    //specify what should happen when the frame
    //first becomes active
    public void windowActivated(WindowEvent e){

        System.out.println("The frame has been activated");
 
    }


    //specify what should happen when the user
    //is trying to close the frame
    public void windowClosing(WindowEvent e){

        System.out.println("The frame is closing.....");

        /*
   
        The following line of code 
        specifies that the frame should be closed. 
        e.getSource() gets the object that called the
        event (the frame), and the dispose() method disposes
        of it. The purpose of ((Window) at the beginning of 
        this line of code is to cast the object that calls 
        the method into a window 

        */

        ((Window)e.getSource()).dispose();

    }


    //specify what should happen
    //when the frame has been closed
    public void windowClosed(WindowEvent e){

        System.out.println("The frame has been closed");
        System.exit(0);

    }

    
    //specify what should happen
    //when the frame loses focus    
    public void windowDeactivated(WindowEvent e){

        System.out.println("The frame
                            has been deactivated");        

    }

  
    //specify what should happen
    //when the frame is restored from a minimized state
    public void windowDeiconified(WindowEvent e){

        System.out.println("The frame has been restored
                            from a minimized state");

    }


    //specify what should happen
    //when a frame is minimized
    public void windowIconified(WindowEvent e){

        System.out.println("The frame has been minimized");

    }

    //specify what should happen
    //when a frame first becomes visible
    public void windowOpened(WindowEvent e){

        System.out.println("The frame is now visible");

    }

}

In the above example, a class named FrameWithEvents that implements the WindowListener interface is created. Within the class definition, there is a class constructor that creates a frame which is 200 by 200, listens for window events, and is initially visible. Each of the WindowListener interfaces methods are used to perform some action when something happens with the window such as when it is first opened or when it is minimized. Within the main() method of this class, a new instance of the FrameWithEvents class is created.

For action events use the addActionListener() method.

Example:

//create a button
Button aButton = new Button("This is a button");

//specify that this button listens for action events
aButton.addActionListener(this);

For focus events use the addFocusListener() method.

Example:

//create a textbox
TextField aTextField = new TextField("This is a textbox");

//specify that this textbox listens for focus events
aTextField.addFocusListener(this);

For item events use the addItemListener() method.

Example:

//create a checkbox
CheckBox chkbox1 = new CheckBox("This is a checkbox", false);

//specify that this checkbox listens for item events
chkbox1.addItemListener(this);

For adjustment events use the addAdjustmentListener() method.

Example:

//create a scrollbar
ScrollBar sb1 = new ScrollBar();

//specify that this scrollbar 
//listens for adjustment listener events
sb1.addAdjustmentListener(this);

For text events use the addTextListener() method.

Example:

//create a textbox
TextField aTextField = new TextField("This is a textbox");

//specify that this textbox listens for text events
aTextField.addTextListener(this);

For mouse events use the addMouseListener() method.

Example:

//create a frame
Frame aFrame = new Frame();

//specify that this frame listens for mouse events
aFrame.addMouseListener(this);

For mouse motion events use the addMouseMotionListener() method.

Example:

//create a frame
Frame aFrame = new Frame();

//specify that this frame listens for mouse motion events
aFrame.addMouseMotionListener(this);

For key events use the addKeyListener() method.

Example:

//create a textbox
TextField aTextField = new TextField("This is a textbox");

//specify that this textbox listens for key events
aTextField.addKeyListener(this);

NOTE: The purpose of using the this keyword within the method that makes the object listen for events is to specify that the object itself should be affected when the event occurs.

Using Inner Adapter classes

You can also make objects listen for events by using inner adapter classes. Inner adapter classes can be used to facilitate the implementation of various listener interfaces.

Example:

//declare a button
Button aButton = new Button("This is a button");

//specify that this button listens for
//action events using an inner adapter class
aButton.addActionListener(new ActionAdapter(){
public void actionPerformed(ActionEvent e){
count++;
}});

In the above example, a button listens for an action event by implementing an inner adapter class named ActionAdapter.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments