Java Reference
In-Depth Information
Use Anonymous Inner Classes or Lambda Expressions to Handle
Events
Up to this point, the programs in this chapter have used a simple, straightforward approach
to handling events in which the main class of the application has implemented the listener
interface itself and all events are sent to an instance of that class. While this is perfectly ac-
ceptable, it is not the only way to handle events. For example, you could use separate listen-
er classes. Thus, different classes could handle different events and these classes would
be separate from the main class of the application. However, two other approaches offer
powerful alternatives. First, you can implement listeners through the use of anonymous in-
ner classes . Second, in some cases, you can use a lambda expression to handle an event.
Let's look at each approach.
Search WWH ::




Custom Search