What are Java Listeners


A listener in Java is an object that is used to handle events. In effect, it is implemented to listen for events and then tell the program the required information about that event, which you may then handle. For example, if the player moves the mouse, a listener will alert the program that the mouse has been moved and give details of its position, relative to the component currently occupying that area of the screen.

The most commonly used event listeners are shown below and are found in the package java.awt.event.

ActionListener

FocusListener

KeyListener

MouseListener

MouseMotionListener

WindowListener

The following tables show details of the abstract methods defined in these three listeners and the events that invoke them.

KeyListener

void keyPressed(KeyEvent e)

A key is pressed down (these events will continue to occur when the user holds the key down but with a key delay).

void keyReleased(KeyEvent e)

A key is released.

void keyTyped(KeyEvent e)

A key is pressed and then released (note that this event is only posted for keys that are deemed to be type-able, such as alpha characters and numbers and not keys such as F1, Ctrl, and Alt, for example).

MouseListener

void mouseClicked(MouseEvent e)

A mouse button is pressed and then released on a component.

void mouseEntered(MouseEvent e)

Mouse enters a component area.

void mouseExited(MouseEvent e)

Mouse exits a component area.

void mousePressed(MouseEvent e)

A mouse button is pressed on a component.

void mouseReleased(MouseEvent e)

A mouse button is released on a component.

MouseMotionListener

void mouseDragged(MouseEvent e)

A mouse button is held down on a component, and then the mouse is moved.

void mouseMoved(MouseEvent e)

Mouse is moved on a component and no buttons are down.

For convenience, javax.swing.event.MouseInputListener implements all of the methods included in the MouseListener and MouseMotionListener interfaces together.

Information about an event is stored in an event object, which is passed as a parameter to a listener method when it is invoked. This is shown in the previous tables, with the class KeyEvent for events associated with the keyboard and the class MouseEvent for events associated with the mouse.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Gabriela C. Perez at 09022007

Related Articles

1. Java EE Platform
Java is a programming language. The Java EE Platform provides a runtime environment (also known as JRE or Java Runtime Environment) as well as a development kit (also known...

2. Java EE Architecture
The Java EE application program interface (API) consists of a suite of technology components and services that are used to build enterprise applications. It includes compon...

3. ISO Management Elements in Java EE .NET Platforms
In a heterogeneous application and platform environment, IT managers are faced with different and often incompatible management frameworks. IT organizations often partiti...

4. Importing Java Packages
To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O packag...

5. What is a Java Package
A Java package is a collection of related classes that can be imported into your program to support your software. They also provide namespace management, as wel...

6. Character Escape Sequences
Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) ch...

7. Conditional Statements
The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statemen...

8. Java Methods
Methods are used as the building blocks of your program, performing tasks that can be called again and again and using the same code to perform the task each time. The basi...

9. Variable Scope
The scope of a variable is the area in which a variable belongs, specified by the area in which it is declared. The following example code contains two declared variables, ...