Java is truly exceptional because it offers many supports to the programmers. One offering in this sense is AWT package. AWT package helps a programmer to develop window programs very convenient. Conveniently and we have discussed about many features those are there in a durability package. And today we are going to discuss one more important possibly to my understanding is very important one features it is called the event handling.
So, today our topics basically to discuss about the event handling mechanism in Java. This is essential for any window based. A program development, now first we will discuss about the concept of event and as we know exactly what is an event in fact, in the context of any program execution. So, as we know we have already familiar to Few things about say applets and as we see in case of applet there is a keyboard like right and then the applet contents say calculator applet it contains a key. Now that key can be typed or key can be pressed.
Now whenever key can be pressed then it will basically sense that which key has been pressed. Similarly. If we move a mouse and then point a mouse on a component like say button, then and then the button is clicked, then the event is occurs in the sense that button has been clicked and then for this event there should be some execution right and then system will do something whenever a user clicked a button. Now this basically is an event look like.
Now, so far the graphical user interface is concerned even it is look like this, but event has more many implications in many application areas. For example, when you are driving a car for a driver there are many things are to be controlled and actually whenever driver wants to control for example, to break a car. So basically an event he creates. Similarly, if he accelerates then another event is created.
Now in case of driving a car at the same time many events can be generated can be triggered and then the car has that machineries the mechanism by which all the events which occurs at any instant at any moments can be handled. So, there is basically handling the events. by means of some hardware mechanism or by means of some software mechanism. So, event handling that is possible in Java is basically manage the event using some software mechanism and that is also particularly in the context of GUI. Now here exactly this is a small example that you can see as we see here this is one frame or we can say a plate which contains a.
button which is the click me. Now, if the user click this button then what will happen? Automatically the system can sense that user has clicked a button and then it pop up a message another frame may be which is shown here.
So, it is basically the event which may be triggered by the user and corresponding to the event there will be some action that will be performed by the system. So, this is the idea about the events. Now, so far the event different types of events are concerned, there are two all the events can be categorized into two broad categories, they are called the foreground event and background event.
The events that we are going to handle or discuss this is in the context of graphical user interface is called the foreground event. On the other hand, there are some events occurs in our computing system always, they are called the background event like operating systems. handles background event whenever you press a restart button or alter control in it or there is a some response from the timer or some other software when it is executing it faces some execution errors divide by 0 errors. Just like it basically causes some interruption we say and this interruption is basically triggering an event and corresponding the event system know exactly what to do. So, this is called the background event.
Now we will discuss mostly the foreground event and in Java what are the event handling facilities that is most important. Now in the context of graphical user interface as we know a container may contains many components. Here, I have listed whatever the main components are possible that is in GUI like button, checkbox, choice and all these things are there. Now, so here whenever a button is there this basically button, checkbox, choice all these components are treated as a source of event. That mean event can be generated from these points only.
A event can be generated from the scroll bar and event can be generated from the list item. An event can be generated from the checkbox like this. Now whenever a particular source of event is excited that means, triggered then that basically the automatically one program that is there in the Java system which basically watch that whether any event occur in any component or not. Whenever it says that some event has been triggered has been occurred then it generates one event.
And, there are many events corresponding to the many different components for example, as we see in this slides from the button if it is the source it generates action events. Similarly, if it is the list then also it is action events, wherever it is a check item or choice the events that is generated automatically is called the item events. Likewise call bar it is called the adjustment event or text field it is called the text event. So, what I want to say is that all the events are basically categorized according to that here. So, what are the possible events that is possible in a GUI is listed here.
As you see action event, item event, component event and then container event, adjustment event, text event, focus event, windows event, key event and mouse event. The last two events are very much important because usually user gives many event I mean creates your. creates many events using these two device devices namely keyboard and mouse. So, if the event is generated by the keyboard it is called the key events, if the event generated by the mouse it is called the mouse event and you note that they are not exactly the component, but they can interact with the component that is the thing what I want to mention here.
And all the action items for example, related the button, list and then menu item like this. Similarly, item event is related to checkbox choice, checkbox or checkbox menu item list like this. Now, this list is very important you should know that which is the name of the event corresponding to the which is the source. This slide in this sense very important and you should have the complete familiarity about because this event that I have mentioned is created by a particular class which is defined in AWT package. Now I am going to discuss about.
What are the different classes and interfaces those are possible in an event? In nutshell what I want to mention here is that a source like say button or a check box or any windows whatever it is there can generate an event and whenever there is an event occurs it goes to listener items. Now therefore, we should have the two things in your mind that event and corresponding to event are listeners. Now, what exactly the listener it is? Now event will be triggered by the user and then there should be some mechanism by which if an event occurs any time at any moment it should be listens by someone else.
So, listener is another what is called the object I should say which basically listens to any event if it occurs at any instant anywhere. Now so, there is a listener is watchful to receive any event if occurs. class in the GUIs in any windows elements.
Now, in Java all these events are basically that mean that the see basically whenever a source the source can create an event. So, it basically create an object actually the object is of type event and those object is defined I mean type of the object is basically event this is defined in java.awt.event is a class. So, there everything is defined there. Now, here the most important thing so far the java dot awt package is concerned is that all the classes and interfaces which are basically responsible for either creating event whenever a source is interacted by a user.
Or whenever an event is created it is listened by the listener. So, these two things that means, the event creation and then listening an event is basically handled by two items in the Java that classes and interfaces which are defined in java.awt package. Now, I will discuss first about event handling class.
Truly there are many different type of events possible and according to these events each events there are different. are there. Now, for example, there is one class called action event.
Now, action event is class basically responsible to create any action event. Now, action event occurs because of the button, because of the checkbox, because of the choice like this one. So, as we have already discussed that this is a different event from this different source. Now, here again this is a container adapter, this is another type of event that occurs from the container itself if there are something changes happens.
or that means, any event is triggered there. So, likewise there are few more classes also here and I have listed all the classes that is for your information and I should advise you to go through all the classes those are there which basically causes an event. And whenever you are whenever dealing with a particular elements, particular components then corresponding to that particular components which event may occurs and corresponding that event which class is responsible that information you should have. So, that you can do all the programming correctly and effectively.
So, these are the classes that I have already listed here similar to the classes there are ok I will just discuss about action event classes those are there is basically handling any action event which may occur whenever a user clicked a button or a list item or menu item these are the things. So, this is related to all these components. Now that.
class details I do not want to discuss elaborately in this I have mentioned the slides here. I advise you to go through the slides read everything and what I can say is that for every class that is there in related to the handling event there is a constructor, there is some constants involved and some methods involved. And for this action event as we see here in this slides three constructors are there and this constructor is basically as you know the action event is automatically.
So, there is no need to call create any object explicitly that means, if you click a button automatically the action event object will be created and we have to just exactly hold this objects for our further processing. We will demonstrate we will illustrate the concept clearly whenever we will discuss some example. So, for this action event these are the constructors apart from these constructors there are only these constructors are there is no other methods and.
All these things for this action event. Now, this is the another adjustment event, it is the event that is related to the scrollbar item. Whenever user negotiates with the scrollbar, then this event occurs and also this class deals with constraints and then constructors, only one constructor and two methods are there.
Now component event, whenever some component is changed or component is resized, component is configured and all these things is there. So, component event is involved and these are the constructor there which is there in the component event and then constants also few constant as we have mentioned component hidden, component moved, component resize, component shown. Here component means a frame like.
So, if you resize a frame it basically trigger an event. If you component is closed there is a event occurs. So, corresponding event it will basically trigger some action that action can be listened by some listener objects like.
How So, like this container event is also there, a container is similar to the component. So, here either container can be added or container can be removed. Whenever you close that event occurs, whenever is open that time it will occur like this. The focus event is basically whenever there is a focus, a keyboard needs to be focused like whenever some event that has to be listened to like this. So, focus event is there, it has the few values.
constant it is called and then constructors and methods. And then input event is basically the superclass is a superclass of the key event and mouse event which we are going to discuss shortly. And this is the two most important event handling mechanism by which the key and mouse can be controlled. And item event is also item event is related to the menu item or list or checkbox and dealing with this and this is this class contains some constants constructors and method. which has been mentioned in the slides.
And then key event, key event means whenever there is a key press or there is a key release. or just typing a key. So, these are the three things that may occur they are called modifiers. So, whenever some the things is occur either key pressed or key released or key tape this event is generated and for this event there are two constructors as we see here the two constructors have that different parameters all those parameters has their own meaning. So, component source means from which component it occurs because there may be more than one component in a program it is exact.
And integer type means what type of component whether key pressed or key released or key typing. Long when means it will at what time that event occurs. And integer modifiers means what is the modifier mode actually.
And integer code that means what is the character that can be coded from this key types and a character key if it is a key type like this one. So, these are the different what is called the parameters that is there in the constructor. What I want to say is that whenever one key presses occurs.
All these values are basically initialized I mean object is created with all these values actually and those values are very important to use in our program. Now mouse event like this key event mouse event has few constants as we have mentioned here mouse click, mouse drag, mouse enter, mouse exited, mouse moved, mouse released, mouse pressed and mouse wheel. Mouse wheel is for the 3 button mouse actually.
Now so these are the Now, things that may happen that means, if out of many types. So, anyone of these things related to the mouse may occurs and those things can be handled and there are two methods a gate key care and then gate key code these are the basically if you select one character then which is the gate key all these things can be selected and can be obtained and it has only one constructor as the name of the constructor is mouse event same as the mark. name of the class and the different parameters that is there in the constructor it is there. So, these are the constructor means these parameters can be generated automatically whenever event occurs and then the value. For example, if you want to note that at what time mouse has been clicked, I can get it from the long when.
So, when value gives you that if there is a mouse clicks, a mouse pressed or mouse release then at what is the time. So, these are the very important information that can be obtained. in text, scene to eye that means, in which location in the component the mouse has been clicked or released those kind of things are there.
So, those values are very essential whenever we have to process the event and text event is related to the handling the text field area. So, it has only one values text value changed or not and then is the true or false like and it has only one constructor that the constructor is basically what is the source and what is the type of the things there. And these are last on.
one class that is there is called the window event, it has also few constructor and then constraints methods all these thing related to this one. Now related to these classes so far the event handling is concerned whenever an event occur the event of any type of the class which I have discussed and object of that type will be automatically created that is all. Now this is so far the event so event occurrence is concerned.
Now there is a For every event there should be certain what is called the consequence. Now this consequence like event generation it is not automatic. So, user has to mention that what is the consequence or what is the action that should be corresponding to an event.
So, interface basically gives a programmer facility to define their actions for every what is called the events occurrence. Now here the different. interfaces are there, those are defined already in the java.AWT package.
As you know interface is basically there is method is there, but methods are abstract. So, we have to implement the methods, we have to define the methods according to our own need that means, if this event occur then what will be the action the consequence that you have to discuss. So, using this interface object. You will be able to create our methods provided that we know the name of the methods which are to be defined precisely corresponding to each event. Now like say actionListener is an interface only the method it is there actionPerformed actionEventAe.
So, that is basically which action event that is the object automatically created we have to pass an argument to that and then actionPerformed interface will be defined accordingly. And then And the adjustment listener is basically the method is their value change that needs to be defined by. The component listener has many methods like component resize, component move, component shown, component hidden and those methods are needs to be implemented. Similarly, container listener it has only two methods component added and component remove this has to be considered and then object that has to be passed is basically container event. And then focus listener the object that has to be passed as a focused event that means, this listener related to the focus.
event, and then focus gained or focus lost is the method. And then itemListener is the focus change is the method needs to be overwritten. And then keyListener is the key pressed or key released or key typed. These are the three I mean methods that user can modify in their program. So, that according to key pressed what will be the action, according to the key released what will be the action, if a key is typed then what will be the action like this one.
So, method can be customized by the programmer according to their needs. KeyListener is another important listener is there, it has method mouseClick, mouseEnter, mouseExited, mousePressed and mouseRelease those methods are need to be modified and mouseDragged and mouseMoved also are that two another interface method. MouseWheelListener the three button mouse you know.
So, the mouse wheel. So, mouse wheel have the mouse wheel method is the listener here. And, textListener only one method text changed. WindowFocusListener it has two methods windowGainFocus, windowLossFocus and then windowListener is a many method as we have mentioned windowsActivated, windowClosed, windowClosing, windowDeactivated, windowDeiconified, windowIconified and windowOpened.
Now, these are the interfaces we have mentioned here and it is also not possible to discuss each event classes and corresponding to each event classes. What are the actions our listeners are possible because it is too time consuming. You are advised to refer to some materials that is I have already mentioned in the first week of my lecture slides where I have given very number of sources you can consult all the sources to have the full knowledge about it. That is ok this way only you can learn more much and much, but this is at the starting point of course. So, at the starting point you should have the full concept about what are the different classes.
are there to dealing with the events, what are the different what interfaces are there to generate the event consequence all these things are there. What are the methods and everything that you can have in your repository in your disposal, so that you can process them and accordingly the program can be written. So, I will give you some examples, so that you can understand about how the events can be handled using this one. As the time is short, I will not be able to cover all.
type of event source only mouse and keyboard are the two events will be concerned. Now anyway, so what is the basic idea if you have to write a program dealing with the event handling. So, there are only two steps these are the very simple two steps. In the first step that is called the registration procedure here basically we have to register and event listener.
So, here the idea about. Just simply in the if you use the applet programming, then in the init method you just add one method like public void add type of listener, whether is the action listener, is a mouse listener or keyboard listener, key listener or like this one or mouse motion listener whatever it is there. So, public void add say key listener that means, we are adding to key listener and what is the type of the object that it can handle for example, action event for the key. Mouse event for the mouse this kind of things are there. So, it is like this and then implement the listener method.
So, if it is suppose action event then the method that you have to implement whether key press, key down, key release whatever it is related to this one. Similarly, if it is the add mouse listener is the listener interface that means, and then mouse event is the event occurs then the we have to implement the method like say mouse pressed, mouse dragged. mouse released, mouse moved whatever it is there. So that means, what will happen if there is a mouse pressed, mouse click and accordingly we have to write the listener method.
So that if it happens then what will be the actions or that I mean event corresponding event what will be the action to be performed. So that is that. So, these are two things are there.
So, registration and then implementation. So better some illustration is required so that you can understand about these two process. Anyway, so as you have just I summarizing this for fact is that whenever an event occurs that means, a user interacts any source like say button or any list or items whatever it is there automatically.
to this there will automatically an event will be created. And this event will be created corresponding to this event there are many other information which automatically stored in the objects. And then event object is forwarded to the listener method which is already registered in the program. So, you can register as many as listener as it is possible or relevant so far your program is concerned.
So, you just have to do it. And here is. Let us have some examples so that you can understand about it. First I will discuss about handling mouse events and so far the mouse event is concerned as we have already mentioned these are the mouse events that may happen mouse click, mouse drag, mouse enter like, like, like and these are the basically field value that can be.
That means, if every mouse event occur then these are the on value should be can be read by the program. So, that program programmer can understand which it occurs because. we have to do it automatically.
And then there is a constructor which basically automatically the mouse event object will be created by this these are the different value that we can process it. And then the methods that is possible so, get x, get y that mean in which location a mouse click occur that can be obtained like this one. Now, there are two I mean interface as you have already learned about mouse listener and mouse motion listener interface so, that means, if it is a mouse click. mouse press, mouse release, the mouse listener. If the mouse dragging is involved then mouse motion listener interface are to be registered in your program.
And so, far the mouse listener interface is concerned these are the method already you have mentioned and for the mouse motion listener these are the two methods already there this means we have to implement it. Now, so, far the dealing with the mouse event handling. So, first what we have to do is that either in init method or any beginning of any class declaration that is related to event handling program. we have to add these two one. So, add mouse listener, add mouse motion listener and then this is basically for the current object, current object actually.
So, this is the public void add mouse listener there should not be any blank space actually please. So, blank space should be avoided. And then the implement the interface method as per the requirement of the programmer and that can be done immediately after the init methods.
So, that is the two procedures is there. Now, let us have the examples for these things. As we see this is a small example that we are going to discuss is a scribble on applet and as you know the scribble applet is basically allow the user to I mean draw something on the some component or container on frame or maybe on applet.
So, in this example as we see we are declaring one class called the scribble on extends applet. So, it is a basically applet. So, applet will be there and then it implements mouseListener and then mouseMotionListener.
So, is a two interfaces are involved in this content. So, this is a customary thing that we have to do it and these are the x and y are the two variables that needs to be maintained. So, we have discussed it here. And now let us see the public the init method.
In this init method we have registered the two interface two listener actually here the addMouseListener and addMouseMotionListener objects are to be. So, this basically include the registration there and this registration followed by the declaration of the interface methods namely mouse pressed and then mouse dragged the two methods we have implemented here. So, it is very simple it will basically get the current location of x value and the current location of y value and then it will store into last x and last y. So, that is all. So, this whenever mouse pressed is there it will just read what is the value of x and y it is there.
And, you know that E is the event so, that event means whenever this plates will occur say it will corresponding to this event that event E will be generated. So, that this will basically return this object event is there. Now so, or here basically mouse space mouse event E as we see the whenever mouse event occurs so, an event E is created and this is basically for that event the x and y location are there. coordinates of the x and y position in the area.
And then mouse drag it is basically what we have done is so, basically if this is the last position and this is the current position. So, drag means they draw a line. So, if the mouse is move here and here, so it is will draw a line automatically.
So, here like line will be draw. So, if we mouse move and like this then automatically the line will be drawn by the system and this is the routine it is here we have mentioned here. So, it basically So, just a graphics object is created that means, it is basically related to the drawing a graphics means line drawing will be there and that is why g.line we have then and for this g.line we see last text last.
So, it is basically what is the previous x y and that is the current x y draw line. So, previous x y current x y like this. So, this way you just dragging and as the dragging continues it will draw the line whether the straight line or any curve a line whatever it is there.
And so, these are the methods so far the mouse pressed and then mouse drag is concerned. Now, there are few other methods also so far the mouse even is concerned like mouse click. mouseEnter, mouseExited, mousePressed, mousePressed and mouseReleased, mousePressed is there already we have declared.
So, it is no more required and so, these are the other methods those are remaining. Now here I just do not have any code for this method, the remains a void method like. So, otherwise you can just simply ignore all this, the only two methods are involved then you can define the two methods and this completes the scribble applets.
And if you. If you run the scribble applet you can see it will give the output and that will be discussed whenever we have the complete demonstration. Now another example let us see the scribble 2 applets it is little bit earlier one it is basically using the latest java JDK version. So, and this is the one example which is basically old one using the java 1.0 version, but it is pretty useful and then very simple little bit the different methods and classes are there. I will quickly mention about.
So, here basically as we see in this example, we have to create one button called the clear and also we have to create one checkbox. The checkbox will if you click it, this checkbox will give you the number of colors that is there. So, user if can clear it, then basically it will whatever the user has tried it will be clean from here. And whenever user click this one, so a list item will be generated. From this list time if the user can select any item corresponding to item the color will be selected.
So, here as we see the two components is created one is called the button and then another is called the current color choice. So, these are the two components are created here button this is for the button and this is the choice is a choice basically are the this one. So, these two buttons are created.
Now in the init method as we see. we have just simply set background all these thing we have already known about here and these are the variable that is declared they have been initialized that one. And then all the buttons and then choices those are declared they have been added into the same it is basically this is applet.
So, it is added to the applet and then finally, we just create the choices. So, this basically code to create the choice component it has the so many elements in it and then finally, we add the choice into the. So, this is the method that we have declared here. Now here see this is basically the method that we have to overwrite.
This method is defined in java.awtMouseEventDeclaration and here event e int x int y is basically showing there what is the event that means if it is a mouse event and x and y means in which location the mouse event occurs. So, this is basically the method as per the old version of the java 1.0 package. And, then it is basically same as the earlier one. So, this basically define mouse down that means, if a mouse is clicks and this is a mouse drag it is similar the mouse drag listener method actually that we have already discussed in the last example event X and Y is the same thing.
So, it is there set color basically if the color is chosen by this. So, current color set color according to this color it will draw. That means, here as we see user can set a color and according to this color if it is selected.
drawn then it will draw like. So, if it is a pink color and then it is pink color will be drawn and so on. So, there is a very nice applet is very simple only few lines codes are there and that is why this AWD programming is called a light weight process. You see you can find many things which is automatically done by you only you have to just play and plug that is all.
And which is basically not possible any other programming language actually that you can do it from here. So, this is the idea about the two examples. So, Mouse Event Handling is concerned. Now I will discuss about another example. This example is MouseListener is very same similar example I will just quickly have it.
So, addMouseListener is basically MouseListener in this case this is basically creating the graphics for us that means, this is a frame here it is basically we create a frame in this example. And here these are the method that we have overwritten. It is very simple mouse event method mouse click it will just simply mouse click and like that. So, this way it will.
just whenever some clicks are occurrence click is occurred. So, it will basically response to that and then object will be created like this kind of output you can get it. Now handling keyboard event this is the let us see how the keyboard can be handled. Here as we have all discussed is basically whenever keyboard event occurs there is a key event object will be created and this object has the three different values are there fields key press key released and key typed.
And, then it also has the many other field also there. So, far the different keys are concerned because in your keyboard whatever the poetry keyboard that we use it in general, it has many function keys alter, control, shift, then modifier and then some are non-functional keys and then character keys, numeric keys all these things are there. So, according to these things it basically deals with the different values that it can read from the keyboard and it has the two methods the get care and then get key.
Basically, what is the character that is in the thing and getKey is basically return the ASCII values right. And so far the listener is concerned it has only one listener keyListener method is there. So, addKeyListener to be accomplished in init method or any at the beginning of any program that you have to implement. And then in addition to this addKeyListener that you have to use also requestFocus method to be include here.
So, these are the two things that needs to be added first and then once it is there this is a registration procedure then rest of the part is implementing the three methods which were relevant to your program execution the key pressed, key released and key typed. Now I will discuss about a small example by which you can understand about these are the methods here. Now in this example as we see here these are the registration process and these are the basically interface implementation. related to the key press event.
As you see whenever key event occur key is automatically generated we can read the values of this key objects whatever the according to the constructor that we have already defined and then we will be able to handle it. So, if this program is run it will basically all the times it will prompt that what are the key events it occurs in your system. And, then this is another example and then if this example is a similar to that one also it has also like registration method as the previous one, but here are different function key that it can be because there are different function it can identify and accordingly it can print it and other key event also key press key release key type for the sake of simplicity I have mentioned here. So, that you can write the code of your own according to this event and then you can get the result here. So, this is basically just starting point we have discussed because event handling is not a simple task to understand so easily, but as the time is short.
So, we do not have any other things to discuss here, but in our next demonstration when we will discuss it lot of many things also will be demonstrated to you. So, that you can understand about it more in a detailed manner. Thank you very much.