Unit - 5 - AWT and Event Handling
Unit - 5 - AWT and Event Handling
AWT (Abstract Window Toolkit) is a set of APIs (Application Programming Interfaces) provided by Java for
creating graphical user interfaces (GUI) for Java applications. It provides a collection of classes and methods
that allow developers to create and manage windows, buttons, text fields, menus, and other GUI components.
AWT is a part of the Java Foundation Classes (JFC) and is built on top of the Java Virtual Machine (JVM). It is
platform-dependent, which means that the appearance and behavior of the GUI components may vary on
different operating systems. AWT was introduced in Java 1.0 and has been replaced by Swing in later versions
of Java.
In Java, event handling is typically done using the ActionListener interface, which defines a single method
called actionPerformed(). This method is called when the user performs an action, such as clicking a button or
selecting an item from a menu. The actionPerformed() method contains the code that responds to the event,
such as updating the GUI or performing some other action.
To implement event handling in Java, you first need to create an object that implements the ActionListener
interface and then register it with the component that generates the event. For example, if you want to handle
button clicks, you would create an ActionListener object and then register it with the button using the
addActionListener() method.
Overall, event handling is an essential part of GUI programming in Java and allows programs to respond to
user actions in a dynamic and interactive way.
At the top of the hierarchy is the java.awt package, which contains the basic classes for creating windows,
panels, buttons, labels, and other GUI components. The java.awt package also includes the Graphics class,
which provides methods for drawing shapes, images, and text on the screen.
Below the java.awt package are several sub-packages that provide more specialized classes and interfaces.
These include:
● java.awt.event: Contains classes and interfaces for handling events, such as mouse clicks and key
presses.
● java.awt.color: Provides classes for working with colors, including Color and ColorSpace.
● java.awt.font: Contains classes for working with fonts, including Font and FontMetrics.
● java.awt.image: Provides classes for working with images, including BufferedImage and
ImageObserver.
● java.awt.geom: Contains classes for working with geometric shapes, including Point2D, Rectangle2D,
and Path2D.
Overall, the AWT class hierarchy provides a comprehensive set of tools for building GUIs in Java, and has
been an important part of Java development since its introduction in the early 1990s.
In Java, events are handled using the event handling mechanism, which involves three main components: the
event source, the event object, and the event listener. The event source is the object that generates the event,
such as a button or a text field. The event object contains information about the event, such as the type of
event and any relevant data. The event listener is the object that receives and handles the event.
To handle events in Java, you need to create an event listener and register it with the event source. The
listener will then be notified when the event occurs, and can perform any necessary actions in response.
Java provides a number of built-in event classes, such as ActionEvent, MouseEvent, and KeyEvent, which
represent different types of events that can occur in a GUI application. These classes provide specific
information about the event, such as the location of a mouse click or the key that was pressed.
Overall, event handling is an important aspect of GUI programming in Java, and allows programs to respond
dynamically to user actions and system events.
1. ActionEvent - This class represents an action that occurred, such as a button being clicked.
2. MouseEvent - This class represents a mouse event, such as a mouse click, drag, or movement.
3. KeyEvent - This class represents a keyboard event, such as a key being pressed or released.
Each event class in Java contains methods and properties that provide information about the event, such as
the location of a mouse click or the key that was pressed. These event classes are used in conjunction with
event listeners, which are responsible for detecting and responding to events in a GUI application.
Overall, event classes are an important aspect of GUI programming in Java, and they allow programs to
respond dynamically to user actions and system events.
There are several types of event listeners in Java, including ActionListener, MouseListener, KeyListener, and
WindowListener, each of which is responsible for handling a specific type of event. By using event listeners,
GUI programs can provide a rich and responsive user experience, allowing users to interact with the
application in a natural and intuitive way.
In Java, event listeners are implemented as interfaces that define a set of methods that must be implemented
by any class that wants to handle a particular type of event. For example, the ActionListener interface defines a
single method, actionPerformed(), that must be implemented by any class that wants to handle action events,
such as clicking a button or selecting an item from a drop-down menu.
To use an event listener in a Java application, you must first create an instance of the listener class and
register it with the component that will generate the events. For example, to handle button clicks, you would
create an instance of the ActionListener class and register it with the button using the addActionListener()
method.
When the event occurs, the component that generated the event will call the appropriate method in the
registered listener object, passing in any relevant data about the event. The listener can then use this data to
update the application state or perform some other action in response to the event.
Overall, event listeners are a powerful tool for creating dynamic and interactive GUI applications in Java,
allowing developers to respond to user input in real-time and provide a rich user experience.
When an event occurs, the event source sends the event to all registered listeners. The listeners can then
respond to the event by performing some action, such as updating the application state or displaying a
message to the user.
The relationship between event sources and listeners is based on the observer pattern. The event source is
the subject or observable, while the listener is the observer. The event source maintains a list of registered
listeners and notifies them when an event occurs.
This decoupling of the event source and listener allows for greater flexibility and modularity in the design of
Java applications. Developers can easily add or remove listeners without affecting the event source, and
multiple listeners can be registered to respond to the same event.
The Delegation event model is based on the concept of the Observer pattern, where an object, called the
subject, maintains a list of its dependents, called observers, and notifies them automatically of any state
changes. In the Delegation event model, the subject is the event source, and the observers are the event
listeners.
The main advantage of the Delegation event model is that it allows for greater flexibility and modularity in the
design of Java applications. Developers can easily add or remove event delegates without affecting the event
source, and multiple event delegates can be registered to respond to the same event. This makes it easier to
maintain and extend the application over time.
To create a GUI application using AWT, developers can follow these steps:
For example, to create a simple GUI application with a button and a label, the following code can be used:
import java.awt.*;
import java.awt.event.*;
public MyGUIApp() {
myButton = new Button("Click me!");
myLabel = new Label("Hello, world!");
add(myButton);
add(myLabel);
myButton.addActionListener(this);
setSize(300, 200);
setLocation(100, 100);
setVisible(true);
}
This code creates a frame with a button and a label, and sets the label to display "Hello, world!" by default.
When the button is clicked, the actionPerformed() method is called, which changes the label's text to "Button
clicked!".