What are Java Listeners

by Gabriela C. Perez.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Computers and technology » JAVA

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.

Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Java Tactics: Earn A Fortune With Java - Get the right Knowledge Get the right Papers Get the right Direction Get the Money Hi, my name's (more...)
Introduction to Object Oriented Programming (OOP) - The transition from a procedural programming (non-OOP) language to an object-oriented programming language is a large step for many programmers. It is true that both methods of programming can ulti (more...)
Operator Precedence in programming languages - Operator precedence deciphers the order in which calculations in an expression occur. Looking at the calculation example 3 + 4 * 6, the answer could be calculated by adding 3 and 4, which gives (more...)
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Bitwise Operators - The following table shows the standard bitwise operators in Java and a description of them. (more...)
Invocation Chaining - Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given stateme (more...)
Regular Expressions in Java - A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters a (more...)
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 (\) character, followed by th (more...)
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 statements, we must first learn a (more...)
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 basic but fundamental parts o (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.