Title: Listeners
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Listener Interfaces

There is a very strong naming convention with the Listener Interface. For (almost) every Interface type the following convention is adhered to:

Interface Name = xxxxListener

Associated Event Name xxxxEvent. The only break in the naming convention is the fact that there is no MouseMotionEvent. Instead MouseEvent is used for the MouseMotionListener Interface. This exception is marked in the following table, by the color red. Note that MouseEvent is also used for the MouseListener Interface.

Component member function (i.e. member of Button Class) for adding the Listener Interface = addxxxxListener

Component member function for removing the Listener Interface = removexxxxListener

If the Listener interface has more than one member function, then there should be an adapter class with a name = xxxxAdapter.

Key Names | Listener Member Functions | Used In
ActionListener

ActionEvent

addActionListener

removeActionListener
actionPerformed(ActionEvent)
JButton, JTextField, etc.
AdjustmentListener

AdjustmentEvent

addAdjustmentListener

removeAdjustmentListener
adjustmentValueChanged(AdjustmentEvent)
Scrollbar and anything that implements the Adjustable Interface
ComponentListener

ComponentEvent

addComponentListener

removeComponentListener

ComponentAdapter
componentHidden(ComponentEvent)

componentShown(ComponentEvent)

componentMoved(ComponentEvent)

componentResized(ComponentEvent)
Component and its derivatives, including JButton, JApplet, JWindow, etc.
ContainerListener

ContainerEvent

addContainerListener

removeContainerListener

ContainerAdapter
componentAdded(ContainerEvent)

componentRemoved(ContainerEvent)
Container and its derivatives, including panel, JApplet, JFrame, etc.
FocusListener

FocusEvent

addFocusListener

removeFocusListener

FocusAdapter
focusGained(FocusEvent)

focusLost(FocusEvent)
Component and its derivatives, including JButton, JApplet, JWindow, etc.
KeyListener

KeyEvent

addKeyListener

removeKeyListener

KeyAdapter
keyPressed(KeyEvent)

keyReleased(KeyEvent)

keyTyped(KeyEvent)
Component and its derivatives, including JButton, JApplet, JWindow, etc.
ItemListener

ItemEvent

addItemListener

removeItemListener
itemStateChanged(ItemEvent)
Checkbox, CheckboxMenuItem, Choice, List, and anything that implements the ItemSelectable interface
MouseListener

MouseEvent

addMouseListener

removeMouseListener

MouseAdapter
mouseClicked(MouseEvent)

mouseEntered(MouseEvent)

mouseExited(MouseEvent)

mousePressed(MouseEvent)

mouseReleased(MouseEvent)
Component and its derivatives, including JButton, JApplet, JWindow, etc.
MouseMotionListener

MouseEvent

addMouseMotionListener

removeMouseMotionListener

MouseMotionAdapter
mouseDragged(MouseEvent)

mouseMoved(MouseEvent)
Component and its derivatives, including JButton, JApplet, JWindow, etc.
TextListener

TextEvent

addTextListener

removeTextListener
textValueChanged(TextEvent)
Anything derived from JTextComponent including JTextArea and JTextField
WindowListener

WindowEvent

addWindowListener

removeWindowListener

WindowAdapter
windowOpened(WindowEvent)

windowClosing(WindowEvent)

windowClosed(WindowEvent)

windowActivated(WindowEvent)

windowDeactivated(WindowEvent)

windowIconified(WindowEvent)

windowDeiconified(WindowEvent)
JWindow and its derivatives, including JFrame, etc.

Events

The top level Event class is found in java.util.EventObject

EventObject which is derived from Object is the top level class for all events.

EventObject has the following member functions:

public Object getSource()

public String toString()

AwtEvent is derived from EventObject and is the parent of the useful Events used in the AWT.

The following is a small list of some of the useful events derived from AwtEvent and some of their useful member functions.

ActionEvent

public String getActionCommand()

MouseEvent

public Point getPoint()

public int getClickCount()

KeyEvent

public char getKeyChar()

public static String getKeyText(int keyCode)