3 Event Handling

Advanced Java Programming · Unit 3 · 4 hrs

Event Handling

Exam-focused notes for Event Handling (Advanced Java Programming, CSC419): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

What this unit covers

  • Event Handling Concept, Listener Interfaces, Using Action Commands, Adapter Classes
  • Handling Action Events, Key Events, Focus Events, Mouse Event, Window Event, Item Events

Handling Action Events, Key Events, Focus Events, Mouse Event, Window Event, Item Events

20815 marks

Describe any two types of events. [5]

In Java, an event is an object that describes a state change in a source. Events are generated when a user interacts with GUI components such as clicking a button, pressing a key, moving the mouse, etc. Java uses the Delegation Event Model to handle events....

Full solved answer →
20795 marks

Why do we need event handling? Explain the use of action event with example. [5]

Event handling is needed in Java to make Graphical User Interface (GUI) applications interactive and responsive. Without event handling, a GUI program would simply display components (buttons, text fields, etc.) but would not respond to any user actions. Ke...

Full solved answer →
207610 marks

Write a Java program to find the sum of two numbers using swing components. Use text fields for input and output. Your program displays output if you press any key in keyboard. Use key adapter to handle events.[10]

This program uses Swing components (JTextField, JLabel, JFrame) for GUI and a KeyAdapter to handle keyboard events. Whenever the user presses any key in either input field, the sum is automatically calculated and displayed in the output text field. Note: Th...

Full solved answer →

Event Handling Concept, Listener Interfaces, Using Action Commands, Adapter Classes

20785 marks

How can we use listener interface to handle events? Compare listener interface with adapter class. [5]

A listener interface is a predefined interface in Java (in java.awt.event package) that contains abstract methods corresponding to specific events. To handle events using a listener interface, the following steps are followed: Step 1: Implement the listener...

Full solved answer →
20775 marks

Define event delegation model. Why do we need adapter class in event handling? [5]

The Event Delegation Model is the standard event handling mechanism used in Java AWT and Swing. In this model, the task of handling an event is delegated from the source (component that generates the event) to a separate object called the listener (or handl...

Full solved answer →