Back to Browse

EVENT HANDLING IN JAVA - Dr Joshila Grace

222 views
Apr 21, 2026
8:15

Event handling in Java is the mechanism that controls how a program responds to user interactions like mouse clicks, key presses, or window resizing. It is primarily built on the Delegation Event Model. The Delegation Event Model This model defines a clear separation between the code that generates the event and the code that handles it. It involves three main components: ● Event Source: The GUI component (like a JButton, JTextField, or Checkbox) where the interaction occurs. ● Event Object: An object created by the source that encapsulates all the details about the event (e.g., which key was pressed, coordinates of a click). ● Event Listener: An interface that "listens" for specific events. When an event occurs, the source invokes a predefined method in the listener. Key Event Classes and Their Listeners The most frequently used event classes and listeners in the java.awt.event and javax.swing.eventpackages include: ● ActionEvent: Triggered by a primary action on a component (e.g., clicking a JButton or pressing Enter in a JTextField). It is handled by the ActionListener. ● MouseEvent: Generated by mouse movements, clicks, or scrolls. Handled by MouseListener (for clicks/entry/exit) and MouseMotionListener (for dragging/moving). ● KeyEvent: Generated when the user types on the keyboard while a component has focus. Handled by the KeyListener. ● WindowEvent: Triggered when a window (like a JFrame) is opened, closed, activated, or deactivated. Handled by the WindowListener. ● ItemEvent: Generated when an item is selected or deselected from a list, checkbox, or choice menu. Handled by the ItemListener. How to Implement Event Handling To handle an event in a Java application (like Swing), you typically follow these three steps: Step A: Implement the Listener Interface You create a class (or use an anonymous inner class) that implements the required listener. Step B: Register the Listener You must tell the source component which object is responsible for handling its events using a registration method. Step C: Define the Logic The code inside the listener's method (e.g., actionPerformed) executes whenever the event is triggered. #sist #sathyabama #java #event #eventhandling #eventsource #eventlisteners #javaevent #mouseevent #keyboardevent

Download

0 formats

No download links available.

EVENT HANDLING IN JAVA - Dr Joshila Grace | NatokHD