< Prev
Next >

Events in Java

User interaction with the elements of graphical user interface(GUI) of a Java program leads to events. These elements could be a button, checkbox, radio button, menu, list, table, scrollbar, mouse, window etc. Such elements are also known as source of an event and there is a specific class corresponding to each source and each event.

Some of the events could be -

Delegation Event Model

Java follows a Delegation Event Model to handle the such events. The two main components of this model are as follows -

Interaction with elements of GUI raises events, which are nothing but objects of classes. EventObject is the superclass of all the events. Let's see a table containing some different kinds of event classes and the description of their events.
Event Classes Description
ActionEvent ActionEvent is generated when a source such as a button is clicked, an item in the list is double-clicked or when a menu item is selected.
ItemEvent ItemEvent is generated when a source such as a checkbox is clicked to check/uncheck it or when a list item is clicked.
KeyEvent KeyEvent is generated when a source such as a key on the keyboard pressed in a textfield or a textarea.
MouseEvent MouseEvent is generated when a source such a mouse is moved, dragged, enters/exits a specific window or when it's button is clicked/released.
TextEvent TextEvent is generated when a data in textarea or textfield is changed.
AdjustmentEvent AdjustmentEvent is generated when a scrollbar is dragged around.
WindowEvent WindowEvent is generated when a source such as a window is activated, minimized, brought up, deactivated and closed.

Let's see a table of some important event classes and their corresponding interfaces that should be implemented by classes in order to listen and respond to the events.

Event Classes

Event Listener Interface

ActionEvent ActionListener
ItemEvent ItemListener
KeyEvent KeyListener
MouseEvent MouseListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener

Looking at the two tables, a class wishing to respond to an event of type KeyEvent, which is generated when a user pressed a key in textfield or textarea, should -

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< Properties Class
ActionListener >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next