Event

Java Events Are Always Paired with Equivalent Listeners

Smiling business woman typing on the laptop
Kristian Sekulic/E+/Getty Images

An event in Java is an object that is created when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers, creating the relevant event object. This behavior is part of Java's Event Handling mechanism and is included in the Swing GUI library. 

For example, let's say we have a JButton.

If a user clicks on the JButton, a button click event is triggered, the event will be created, and it will be sent to the relevant event listener (in this case, the ActionListener). The relevant listener will have implemented code that determines the action to take when the event occurs. 

Note that an event source must be paired with an event listener, or its triggering will result in no action.

How Events Work

Event handling in Java is comprised of two key elements:

  • The event source, which is an object that is created when an event occurs. Java provides several types of these event sources, discussed in the section Types of Events below.
  • The event listener, the object that "listens" for events and processes them when they occur.

There are several types of events and listeners in Java: each type of event is tied to a corresponding listener. For this discussion, let's consider a common type of event, an action event represented by the Java class ActionEvent, which is triggered when a user clicks a button or the item of a list.

 

At the user's action, an ActionEvent object corresponding to the relevant action is created. This object contains both the event source information and the specific action taken by the user. This event object is then passed to the corresponding ActionListener object's method:

 ​void actionPerformed(ActionEvent e)

This method is executed and returns the appropriate GUI response, which might be to open or close a dialog, download a file, provide a digital signature, or any other of the myriad actions available to users in an interface.

Types of Events

Here are some of the most common types of events in Java:

  • ActionEvent: Represents a graphical element being clicked, such as a button or item in a list. Related listener: ActionListener.
  • ContainerEvent: Represents an event that occurs to the GUI's container itself, for example, if a user adds or removes an object from the interface. Related listener: ContainerListener.
  • KeyEvent: Represents an event in which the user presses, types or releases a key. Related listener: KeyListener.
  • WindowEvent: Represents an event relating to a window, for example, when a window is closed, activated or deactivated. Related listener: WindowListener.
  • MouseEvent: Represents any event related to a mouse, such as when a mouse is clicked or pressed. Related listener: MouseListener.

Note that multiple listeners and event sources can interact with one another. For example, multiple events can be registered by a single listener, if they are of the same type. This means that, for a similar set of components that perform the same type of action, one event listener can handle all the events.

Similarly, a single event can be bound to multiple listeners, if that suits the program's design (although that is less common).