|
Another use of inner classes is in event handling of Java GUI applications. Java has event-driven programming in which the programmer can decide how to handle the generated events. When a particular event, say for instance, a mouse-click occurs, the programmer can either
Ignore the event
Handle the event at the component level where the event is generated.
Delegate the event handling to some other object also known as event listeners.
|
|
Java event handling does not come under the SCJP Objectives. But it is one of the important applications where inner classes are used. In fact inner classes were introduced for easing up the event handling in Java. We are discussing the event handling here only for the completeness of the Inner class discussion. |
Let us take an example of simple GUI mail application with a class, MailClient. When a particular event occurs on this GUI say for instance “user clicks on Inbox button”, you can choose to any of the three courses of action that we discussed above. You can simply ignore the event, which is probably not the thing to do in this case. Second option is to let MailClient itself handle these events. However, MailClient is likely to have many components, so MailClient will need to make sure where the event is originated in its event handling method. Following code shows the how the event handling method MailClient would look like:
public void actionPerformed(ActionEvent
e)
{
if(e.getSource()==composeBtn)
{
// compose mail
stuff
}
else if(e.getSource()==inboxBtn)
{
// Display inbox stuff
}
// so on...
}
You would probably agree that this does not look like the right solution as MailClient is supposed to implement behavior such as sending-mail, receive-mail. Instead, it is handling events, which is not directly part of its responsibility. Moreover, the event handling method is clearly an example of bad coding, as it has a lot of if-else statements. The correct solution would be to delegate the event handling to special classes (popularly known as the event listeners). These special classes are dedicated to event handling for the components. It is recommended practice to declare these listeners as inner classes for two reasons.
Event handling often requires access to object’s members. For example, in MailClient class, the inbox button’s event-listener would require to open the inbox and show the arrived mails. Event listeners defined as inner classes can operate directly on the internal variables and methods of a class.
Event listeners are mostly use-and-discard type of objects, which are relevant only to a particular GUI class. Say for instance, MailClient’s event listener may not be suitable as StockBroker’s event listener. Therefore, it is better to have them as inner classes declared in the GUI class.
This approach results in many inner classes for listening different events. However, large switch statements are avoided and event-handling logic is successfully encapsulated. Thus, event listeners as inner classes are better than their equivalent top-level event listeners as they are concise and coded precisely where they are needed, and can access internal variables and methods of enclosing class.