Lesson 9: Event Handling in Swing GUI
Yakov Fain · 2015
Java Swing, like any other user interface (UI) library, is event-driven. When a user interacts with a GUI program a Java Swing program receives an event that can initiate an appropriate reaction. This chapter explains how to handle events in Java GUI programs. To listen to events, a program has to register window components with Java classes called listeners. All listeners are declared Java interfaces and their methods have to be implemented in an object that listens to events. The event listener CalculatorEngine plays the role of the controller. If Calculator needs to communicate with other objects, it should expose a public API to get or set data but hide details about its internals. If a person needs to implement a calculator that remembers the history of calculations, he/she could create a class CalculationsHistory that would serve as a model in model-view controller (MVC).