- A graphical component is a source of any event then that component is called as an event source.
- To handle the particular event for that source we have to register that source.
- After registering the source, all the messages concern with that source are forwarded to a particular class which is called as an actionListener.
- Inside the actionListener we have to provide a definition of a function which are executed when particular event is occurred.
- There are multiple event classes in the java.
- Base class of all the event class is named as event and all the graphical contents are communicate with JVM by using AWT event class.
- When we press any key of a keyboard or mouse, the event of class input event is generated.
- Further that event is classified in mouse event and keyboard event.
Important Events and its Description:-
1) Action Event:-
ListBox is manipulated.
2) Adjustment Event:-
When scroll bar is manipulated.
3) Component Event(It can be a frame also):-
When component is resized, moved, closed, etc.
4) Container Event:-
When component is added or removed.
5) Focus Event:-
When any component loses a focus or gain the focus.
6) I/P Event:-
This class is further classified into multiple classes depend on i/p devices such as mouse and keyboard.
7) Mouse Event:-
mouseMoved, mouseClicked, etc.
8) Text Event:-
When textbox is manipulated.
9) Key Event:-
When user interacts with AWT components through the keyboard.
Important methods of an Event class:-
- This method returns an object of a particular event source
- There are multiple event source such as button, checkbox, list, choice, menu, scrollbar, window, etc.
- this method is used for getting the no of clicks which are performed by using a mouse.
Flow to handle any Event:-
- Create AWT source which can generate an event.
- Create a class which implements Listener interface or extends adapter class.
- Inside that class provides a definition for expected methods of that interface.
- If some methods are not used then we have to provide an empty definition for that method.
Important Listeners and its methods:-
void componentResized(componentEvent e);
void componentMoved(componentEvent e);
void componentShown(componentEvent e);
void componentHidden(componentEvent e);
void componentAdded(containerEvent e);
void componentRemoved(containerEvent e);
void focusGained(focusEvent e);
void focusLost(focusEvent e);
void windowActivated(windowEvent e);
void windowDeactivated(windowEvent e);
void windowOpened(windowEvent e);
void windowClosed(windowEvent e);
void windowIconified(windowEvent e);
void windowDeiconified(windowEvent e);
void keyPressed(keyEvent e);
void keyReleased(keyEvent e);
void keyTyped(keyEvent e);
void mouseClicked(mouseEvent e);
void mouseEntered(mouseEvent e);
void mouseExited(mouseEvent e);
void mousePressed(mouseEvent e);
void mouseReleased(mouseEvent e);
void mouseDrag(mouseMotionListener e);
void mouseMoved(mouseMotionListener e);
Important points about Listener:-
- If we create a class that implements particular interface has to provide definition for all the methods which are provided in that interface.
- That class can provide an empty definition for all that methods.
- If any method is missing in our class then compiler generates an error because we are implementing an interface.
- All the methods of any Listener interface does not have any directly called by the JVM.
- All the function of Listener interface as a single parameter and which is an object of a particular event.
- That object is used for getting further information concern with that event.
Creating our own Listener class:-
class demo implements ActionListener
public void actionPerformed(actionEvent e)
// code which is executed when particular action is performed
System.out.println(“Inside an ActionListener”);
- In this case, demo is Listener class which implements all the methods of that interface.
- We cannot provide any user defined method inside that class.
- Those methods are invoked automatically when particular event is performed by the user.
Creating an Event Source:-
Button b = new Button(“Hello”);
- In this case, we create a new AWT component as a Button.
- After creating an event source if we want to receive the messages which are concern with that message source then we have to register that event source.
Registering an Event Source:-
- In this example, b is an object of event source and by writing a syntax addActionListener we are registering that event source to JVM.
- While registering an event source we have to provide an object of our class which implements a particular listener interface.
- After writing this step all the messages concern with that event source are forwarded to the class which is provided for a parameter as addActionListener.
Removing actionListener from an event source:-
- If we don’t want to receive any messages concern with an event source we have to remove that listener from particular event source.
- After using this syntax there is no event handling for the Button b.
- If we don’t want to receive the message then we can call above method or we can skip the registration step.
- If we want to implement any Listener then we have to provide either empty definition for all methods which are written inside that interface.
- If an interface has 6 methods and if we want to handle single method then we have to provide a definition for single method and empty definitions for remaining 5 methods.
- This Is important step because the class that wants to implements any interface has to provide the definitions for all the methods.
- To avoid this overhead of providing an empty definition for a methods java provides a concept called as an adapter class.
- For each and every listener there is an adapter class.
- In this class there is an empty definition provided for all the methods which are written for a particular interface.in our user defined class, we have to just extend that adapter class and provide the definition for expected methods.
- If we provide our own definition then the empty definition which is provided inside the adapter class is override by our method. If a class is already extended from another class then we cannot extend this adapter class because multiple inheritance is not allowed in java.
- In that case, we have to provide definition for our interface.
class demo extends WindowAdapter
public void windowClosed(windowEvent e)
// code which is to be performed when window is closed
} // there is no need to add remaining methods because we are extending an adapter // calss
- This is preferable to extend an adapter class.
- If we want to provide for only single method but if we want to provide a definition for all the methods then it is preferable to implement an interface.