Javatpoint Logo
Javatpoint Logo

AWT Event Hierarchy in Java

Java's Abstract Window Toolkit (AWT) provides a powerful framework for creating graphical user interfaces (GUIs) in Java applications. One of the fundamental aspects of building GUIs is handling user interactions, such as mouse clicks, keyboard input, and window events. To manage these interactions, Java AWT uses an event-driven programming model, and understanding the AWT event hierarchy is crucial for developing robust and responsive GUI applications.

In this section, we will delve into the AWT event hierarchy in detail, exploring its components, how events are generated, and how to handle them effectively using practical examples.

Java AWT Hierarchy

AWT Event Hierarchy in Java

Component

Component is a general term for all elements, including buttons, text boxes, scroll bars, etc. As seen in the above design, there are classes in Java AWT for each component. Every component must be added to a container in order to be placed in a certain location on a screen.

Container

A component in AWT called the Container can hold other components like buttons, text fields, labels, and so on. The term "container" refers to classes like Frame, Dialogue, and Panel that extend the Container class.

Container Types

In Java AWT, there are four different kinds of containers:

Window

The container without borders or menu bars is called a window. To create a window, you must utilise a frame, dialogue, or another window. To build this container, we must first create a Window class instance.

Panel

The container that lacks a title bar, border, or menu bar is known as a panel. It serves as a general-purpose container for the parts. Other elements, such as a button or text box, may be present. A Panel class instance establishes a container to which components may be added.

Frame

The container that houses the title bar, border, and potential menu bars is known as the Frame. Other parts might include a button, text box, scroll bar, etc. The most popular container for creating an AWT application is the frame.

Event Basics

In the AWT event model, an event is an object that encapsulates specific information about a user's interaction with a graphical component. These interactions can be categorized into various event types, such as mouse events (clicks, movements), keyboard events (key presses, releases), and window events (closing, resizing). Every AWT event is an instance of a class that is a subclass of the java.awt.AWTEvent class.

Event Sources

In the AWT event model, components (e.g., buttons, text fields, windows) are the sources of events. When a user interacts with a component, an event is generated and sent to the appropriate event listener(s). Event listeners are objects that listen for specific types of events and respond accordingly. Java AWT provides a set of listener interfaces, such as ActionListener, MouseListener, KeyListener, and WindowListener, among others.

Event Listeners

To handle events, you need to implement the appropriate listener interfaces and register them with the event source (component). The listener interfaces define callback methods that your code must implement to respond to events. For example, the ActionListener interface includes the actionPerformed(ActionEvent e) method, which you override to specify what should happen when an action event occurs.

AWT Event Hierarchy

Understanding the AWT event hierarchy is essential to grasp how events are propagated through the GUI components. At the root of the hierarchy is the java.util.EventObject class, from which all AWT event classes are derived. Some important branches of the AWT event hierarchy include:

  1. InputEvent: This class represents input events, such as mouse and keyboard events. Subclasses include MouseEvent, KeyEvent, and FocusEvent. For example, MouseEvent represents mouse-related events like clicks and movements.
  2. ActionEvent: This class represents action events and is often used with components like buttons and menu items. When a button is clicked, an ActionEvent is generated.
  3. WindowEvent: This class represents window-related events like opening, closing, or resizing a window.
  4. ComponentEvent: This class represents component-related events, such as resizing or moving a component.

Event Dispatch Thread (EDT)

In Java GUI applications, all event handling code must run on the Event Dispatch Thread (EDT) to ensure thread safety. The EDT is responsible for dispatching events to their corresponding listeners. To work with AWT and Swing, you should be aware of this threading requirement.

1. AWT Label

Constructors:

  1. Label(): Creates an empty label.
  2. Label(String str): Constructs a label with str as its name.
  3. Label(String str, int x): Constructs a label with the specified string and alignment x.

2. AWT Button

Constructors:

  1. Button(): Creates a button with no label (an empty box).
  2. Button(String str): Creates a button with str as a label.

3. AWT TextField

Constructors:

  1. TextField(): Constructs a TextField component.
  2. TextField(String text): Constructs a new text field initialized with the given string text.
  3. TextField(int col): Creates a new empty text field with the given number of columns col.
  4. TextField(String str, int columns): Creates a new text field with the specified str as the initial content and columns as the width.

4. AWT Checkbox

Constructors:

  1. Checkbox(): Creates a checkbox with no label.
  2. Checkbox(String str): Creates a checkbox with str as a label.
  3. Checkbox(String str, boolean state, CheckboxGroup group): Creates a checkbox with str as the label, and sets the state in the specified group.

5. AWT CheckboxGroup

CheckboxGroup is used to group together a set of Checkbox components, enabling the use of radio buttons in AWT.

6. AWT Choice

Constructor:

Choice(): Creates a new choice menu.

7. AWT List

Constructors:

  1. List(): Creates a new list.
  2. List(int row): Creates a list with a given number of rows.
  3. List(int row, boolean multipleMode): Creates a new list initialized to display the given number of rows.

8. AWT Canvas

Constructors:

  1. Canvas(): Creates a new Canvas.
  2. Canvas(GraphicsConfiguration config): Creates a new Canvas with the given GraphicsConfiguration.

9. AWT Scrollbar

Constructors:

  1. Scrollbar(): Creates a new vertical Scrollbar.
  2. Scrollbar(int orientation): Creates a new vertical Scrollbar with the specified orientation.
  3. Scrollbar(int orientation, int value, int visible, int minimum, int maximum): Creates a new scrollbar with the specified orientation, initial value, visible amount, minimum, and maximum values.

10. AWT MenuItem & Menu

MenuItem is used to add simple labeled menu items to menus. Menu is a component used to create dropdown menus that can contain a list of MenuItem components.

11. AWT PopupMenu

PopupMenu is used for dynamically popping up a menu when the user right-clicks or performs other actions on a component.

12. AWT Panel

Panel is a container class used to hold and organize graphical components in a Java application.

13. AWT Toolkit

The Toolkit class provides a platform-independent way to access various system resources and functionalities. Subclasses of Toolkit are used to bind various components.

Example: Handling a Button Click Event

Let's illustrate the AWT event hierarchy with a simple example of handling a button click event.

ButtonClickExample.java

Output:

AWT Event Hierarchy in Java

Explanation

In this example, we create a simple GUI application with a button. We register an ActionListener with the button, and when the button is clicked, it triggers the actionPerformed method, printing "Button Clicked!" to the console.

Conclusion

Understanding the AWT event hierarchy in Java is crucial for building responsive and interactive GUI applications. By mastering the concepts of event sources, listeners, and the event dispatch thread, you can effectively handle various user interactions in your Java applications. The provided example illustrates the fundamental principles of event handling in AWT, but there is much more to explore as you delve deeper into Java's rich GUI capabilities.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA