In this section, you will learn how to handle events in Java awt. Events are the integral part of the java platform. You can see the concepts related to the event handling through the example and use methods through which you can implement the event driven application.
For any event to occur, the objects registers themselves as listeners. No event takes place if there is no listener i.e. nothing happens when an event takes place if there is no listener. No matter how many listeners there are, each and every listener is capable of processing an event. For example, a SimpleButtonEvent applet registers itself as the listener for the button's action events that creates a Button instance.
ActionListener can be implemented by any Class including Applet. One point to remember here is that all the listeners are always notified. Moreover, you can also call AWTEvent.consume() method whenever you don't want an event to be processed further. There is another method which is used by a listener to check for the consumption. The method is isConsumed() method. The processing of the events gets stopped with the consumption of the events by the system once a listener is notified. Consumption only works for InputEvent and its subclasses. Moreover, if you don't want any input from the user through keyboard then you can use consume() method for the KeyEvent.
The step by step procedure of Event handling is as follow:
Most of the times every event-type has Listener interface as Events subclass the AWTEvent class. However, PaintEvent and InputEvent don't have the Listener interface because only the paint() method can be overriden with PaintEvent etc.
A low-level input or window operation is represented by the Low-level events. Types of Low-level events are mouse movement, window opening, a key press etc. For example, three events are generated by typing the letter 'A' on the Keyboard one for releasing, one for pressing, and one for typing. The different type of low-level events and operations that generate each event are show below in the form of a table.
|FocusEvent||Used for Getting/losing focus.|
|MouseEvent||Used for entering, exiting, clicking, dragging, moving, pressing, or releasing.|
|ContainerEvent||Used for Adding/removing component.|
|KeyEvent||Used for releasing, pressing, or typing (both) a key.|
|WindowEvent||Used for opening, deactivating, closing, Iconifying, deiconifying, really closed.|
|ComponentEvent||Used for moving, resizing, hiding, showing.|
The interaction with GUI component is represented by the Semantic events like changing the text of a text field, selecting a button etc. The different events generated by different components is shown below.
|ItemEvent||Used for state changed.|
|ActionEvent||Used for do the command.|
|TextEvent||Used for text changed.|
|AdjustmentEvent||Used for value adjusted.|
If a component is an event source for something then the same happens with its subclasses. The different event sources are represented by the following table.
Every listener interface has at least one event type. Moreover, it also contains a method for each type of event the event class incorporates. For example as discussed earlier, the KeyListener has three methods, one for each type of event that the KeyEvent has: keyTyped(), keyPressed(), and keyReleased().
The Listener interfaces and their methods are as follow:
|ContainerListen er||componentAdded(ContainerEvent e)|
Liked it! Share this Tutorial