UNIT 5
UNIT 5
User Interfaces) or Window-Based Applications in Java. Java AWT is part of the Java
Foundation Classes (JFC) that provides a way to build platform-independent graphical
applications.
In this AWT tutorial, you will learn the basics of the AWT, including how to create
windows, buttons, labels, and text fields. We will also learn how to add event listeners to
components so that they can respond to user input.
Java AWT Basics
Java AWT (Abstract Window Toolkit) is an API used to create Graphical User
Interface (GUI) or Windows-based Java programs and Java AWT components are
platform-dependent, which means they are shown in accordance with the operating
system’s view. AWT is heavyweight, which means that its components consume resources
from the underlying operating system (OS). The java.awt package contains AWT API
classes such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List, and so
on.
Why AWT is Platform Independent?
The Java AWT utilizes the native platform subroutine to create API components
such as TextField, CheckBox, and buttons. This results in a different visual format for these
components on different platforms such as Windows, MAC OS, and Unix. The reason for
this is that each platform has a distinct view of its native components. AWT directly calls
this native subroutine to create the components, resulting in an AWT application
resembling a Windows application on Windows OS, and a Mac application on the MAC
OS. In simpler terms, the AWT application’s appearance adapts to the platform it is running
on.
Understanding how to use BeanFactory is vital for optimizing your application’s
performance. To delve deeper into Java programming, the Java Backend course offers a
comprehensive look at various Java concepts and their applications.
AWT is platform independent even after the AWT components are platform
dependent because of the points mentioned below:
1. JVM (Java Virtual Machine):
As Java Virtual Machine is platform dependent
2. Abstract APIs:
AWT provides an abstract layer for GUI. Java applications interact with AWT
through Abstract API which are platform independent. Abstract API allows Java to isolate
platform-specific details, making code portable across different systems.
3. Platform-Independent Libraries:
The Libraries of AWT are written in Java which they are totally platform-
independent. Because of this, it ensures that AWT functionality remains consistent across
different environments.
Java AWT Hierarchy
Components: AWT provides various components such as buttons, labels, text fields,
checkboxes, etc used for creating GUI elements for Java Applications.
Containers: AWT provides containers like panels, frames, and dialogues to organize
and group components in the Application.
Layout Managers: Layout Managers are responsible for arranging data in the
containers some of the layout managers are BorderLayout, FlowLayout, etc.
Event Handling: AWT allows the user to handle the events like mouse clicks, key
presses, etc. using event listeners and adapters.
Graphics and Drawing: It is the feature of AWT that helps to draw shapes, insert
images and write text in the components of a Java Application.
Types of Containers in Java AWT
There are four types of containers in Java AWT:
1. Window: Window is a top-level container that represents a graphical window or dialog
box. The Window class extends the Container class, which means it can contain other
components, such as buttons, labels, and text fields.
2. Panel: Panel is a container class in Java. It is a lightweight container that can be used
for grouping other components together within a window or a frame.
3. Frame: The Frame is the container that contains the title bar and border and can have
menu bars.
4. Dialog: A dialog box is a temporary window an application creates to retrieve user
input.
1. Java AWT Label
Syntax of AWT Label
public class Label extends Component implements Accessible
AWT Label Class Constructors
There are three types of Java AWT Label Class
1. Label():
Creates 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 x as the specified alignment
9. AWT Scrollbar
Syntax of AWT Scrollbar:
public class Scrollbar extends Component implements Adjustable, Accessible
Java AWT Scrollbar Class Constructors
There are three constructor classes in Java mentioned below:
1. Scrollbar():
It Creates a new vertical Scrollbar in the Application.
2. Scrollbar(int orientation):
Creates a new vertical Scrollbar with the given orientation.
3. Scrollbar(int orientation, int value, int visible, int mini, int maxi):
Creates a new scrollbar with the orientation mentioned with value as the default value
and [mini, maxi] as the lower and higher limit.
3. Java MouseMotionListener
Java MouseMotionListner is a interface which is notified when mouse is moved or dragged.
It contains two Methods mentioned below:
1. mouseDragged(MouseEvent e):
Responds when the mouse is dragged with mouse button clicked over a component in
Application.
2. mouseMoved(MouseEvent e):
Responds when the mouse is moved over a component in Application.
4. Java ItemListener
Java ItemListner is an interface which handles events related to item selection and
deselection those that occur with checkboxes, radio buttons, etc. There is only one Method
associated with ItemListner that is itemStateChanged(). This method provides information
about the event, i.e. source of the event and the changed state.
Syntax of itemStateChanged() method:
itemStateChanged(ItemEvent e)
5. Java KeyListener
Java KeyListner is an interface in Java notified whenever you change the state of key or can
be said for key related events.
Syntax of KeyListener:
public interface KeyListener extends EventListener
There are three methods associated with KeyListner as mentioned below:
1. keyPressed (KeyEvent e):
Responds to the event when key is pressed.
2. keyReleased (KeyEvent e):
Responds to the event when the key is released.
3. keyTyped (KeyEvent e):
Responds to the key has been typed.
6. Java WindowListener
Java WindowListener is a interface used for handling events related to window actions.
Events like opening , closing, minimizing, etc are handled using WindowListener.
Syntax of WindowListener
public interface WindowListener extends EventListener
There are seven methods associated with WindowListener as mentioned below:
1. windowActivated (WindowEvent e):
Responds when window is first opened
2. windowClosed (WindowEvent e):
Responds when the user attempts to close the window
3. windowClosing (WindowEvent e):
Responds after a window has been closed
4. windowDeactivated (WindowEvent e):
Responds when a window is minimized
5. windowDeiconified (WindowEvent e):
Responds when a window is restored from a minimized state
6. windowIconified (WindowEvent e):
Responds when a window is activated
7. windowOpened (WindowEvent e):
Responds when a window loses focus