Graphical User Interface is Event-Driven. We need a mechanism to respond to these events. When an event occurs, we need a mechanism in order to handle this event in this article we will go in detail on how we can listen to events in a very effective way.

What is the Event Listener?

In the graphical User Interface, we usually have more than once components. An Event Listener is a method or class that listen to the event occurring on these components. 

Normally when we want to listen to an event we have to register that component which produces the event. We have to create an event listener some times also called event handler is the class or method, that responds to the events occurring on the components which are registered on that listener or handler.

We can register more than once components with the handler or listener and we more than one handlers or listeners can handle one component. The event listener responds to the events in a very short time. 

How to write an event Listener?

When we are working with the same Graphical user interface all the events that are occurring there should be handled in a very effective way, this is because the methods that respond to these events are executed at the same time, as they are all in the same thread. If a method is taking more time then it can make the application very less responsive. 

if you are working with a huge application then it is always a good idea that, if you have some events that are lightweight, then you have to start a separate thread for those events, because it will be run concurrently, and indirectly improve the responsiveness of your application.

There are many ways to implement Event Listeners, when we are working with a massive application we need to manage these listeners in separate classes, though it is a good idea, it can make your program slower. Though if you go with this method it is an easy architecture then to make your application secure you have to make them hidden by declaring the class is private. 

The methods that are listening to the methods take only one argument, and the argument is the Object of the Event. The Object is of the class Event Object. Now it depends on the methods, each of them is specified for a different type of events. The most important method is the getSource Method.

The getSource method is very useful for us, in terms of getting the information out of the event. The getSource method returns us an Object. we can call other methods on the object returned from the getSource Method. 

We can have many types of events out there in the graphical user interface. There are many differences out there between all these methods. but we have categorized them in two categories. LowLevel Events and Semantic Events. 

What are the Low-Level Events?

LowLevel events are those events which are occurred by window-system or low-level input. we can say that a keypress or a key release or mouse drag, mouse click, mouse release are some examples of low-level Events. 

What are the semantic Events?

Will semantic events are the events that show the interaction with a GUI Component. We can say Action Events are semantic events. like when we click on a button, it fires an action. 

Sometimes we are working with the Graphical user interface and we only need to listen for only one event and not for all events that are occurring there. but the problem sometimes occur is that the listener interface has methods that should be implemented. Like we can have an interface that listens for a DocListner. There are three unimplemented methods that we need to implement in our class, although we could leave the body of the unneeded method empty it will make our code harder to read and our code will be filled with unnecessary code. to avoid such kind of situation we have adapter classes that help us achieve the required results. Look at the picture we have implemented the Document listener now we need to implement the methods.

The adapter classes are the classes they have implemented the interfaces with the empty bodies of the methods. we can extend these adapter classes and override the methods that we need to work with. SO it kinda gives us the ability to write clean code. 

there are adapter classes for each interface out there, if you are not going to implement all the methods and do not need to handle all the events then it is a good practice to use the adapter classes. and they are present for each interface out there. Unfortunately, there is no adapter class for DocumentListner, but we can create our own Adapter class as well.

Use of Innerclasses and Anonymouse classes

While we are working with the Adapters, as we mentioned above. we can extend two classes in one time. So what if we want to extend JFrame as well as MouseAdapter or another adapter. the solution we will have to either use inner class or anonymous class. but the more classes you create the slower will be the performance you have to manage it. 

If you have any queries regarding this article or any other article please post them in the comment section.

If you are new to Java. I recommend you go through some of the below articles it helps you understand java easily.

Java tutorials 

Java Projects

Post a Comment

Previous Post Next Post