What Is Java AWT (Abstract Window Toolkit)

This Java AWT tutorial explains what is Abstract Window Toolkit in Java and related concepts like AWT Color, Point, Graphics, AWT vs Swing, etc:

We got introduced to the basic GUI terms in one of our earlier tutorials. In this tutorial, we will discuss one of the oldest GUI frameworks in Java called “AWT Framework”. AWT is the short form for “Abstract Window Toolkit”.

AWT is an API for creating GUI applications in Java. It is a platform-dependent framework i.e. the GUI components belonging to AWT are not the same across all platforms. As per the native look and feel of the platform, the look and feel of the AWT components’ also change.

=> Check ALL Java Tutorials Here.

Java Abstract Window Toolkit (AWT)

JAVA AWT (Abstract Window Toolkit)

The Java AWT creates components by calling the subroutines of native platforms. Hence, an AWT GUI application will have the look and feel of Windows OS while running on Windows and Mac OS look and feel when running on Mac and so on. This explains the platform dependency of Abstract Window Toolkit applications.

Due to its platform-dependence and a kind of heavyweight nature of its components, it is rarely used in Java applications these days. Besides, there are also newer frameworks like Swing which are light-weight and platform-independent.

Swing has more flexible and powerful components when compared to AWT. Swing provides components similar to Abstract Window Toolkit and also has more advanced components like trees, tabbed panels, etc.

But one thing to note here is that the Java Swing framework is based on the AWT. In other words, Swing is an enhanced API and it extends the Abstract Window Toolkit framework. So before we jump into Swing tutorials, let’s get an overview of this framework.

AWT Hierarchy And Components

Now let’s see how the Abstract Window Toolkit hierarchy in Java looks.

Given below is the diagram of the AWT hierarchy in Java.

AWT Hierarchy in Java

As shown in the above figure the root AWT component ‘Component’ extends from the ‘Object’ class. The component class is the parent of the other components including Label, Button, List, Checkbox, Choice, Container, etc.

A container is further divided into panels and windows. An Applet class derives from Panel while Frame and Dialog derive from the Window component.

Now let’s briefly discuss these components.

Component Class

The Component class is the root of the hierarchy. A Component is an abstract class and is responsible for the current background and foreground colors as well as the current text font.

The component class encapsulates the visual component properties and attributes.

Container

Container AWT components can contain other components like text, labels, buttons, tables, lists, etc. The container keeps a tab on other components that are added to the GUI.

Panel

The panel is a subclass of the Container class. A panel is a concrete class and does not contain the title, border, or menu bar. It is a container to hold the other components. There can be more than one panel in a frame.

Window class

Windows class is a window at the top level and we can use frames or dialogs to create a window. A window does not have borders or menu bars.

Frame

Frame derives from the Window class and can be resized. A frame can contain various components like button, labels, fields, title bar, etc. The frame is used in most of the Abstract Window Toolkit applications.

A frame can be created in two ways:

#1) By using the Frame class object

Here, we create a Frame class object by instantiating the Frame class.

A programming example is given below.

import java.awt.*;  
	class FrameButton{  
		FrameButton (){  
			Frame f=new Frame();  
			Button b=new Button("CLICK_ME");  
			b.setBounds(30,50,80,30);  
			f.add(b);  
			f.setSize(300,300);  
			f.setLayout(null);  
		f.setVisible(true);  
	}  
	public static void main(String args[]){  
		FrameButton f=new FrameButton ();  
	}
}  

Output:

FrameCreation_Object 1

#2) By Extending the Frame class

Here we create a class that extends the Frame class and then create components of the frame in its constructor.

This is shown in the program below.

import java.awt.*;  
class AWTButton extends Frame{  
	AWTButton (){  
	Button b=new Button("AWTButton");  
	b.setBounds(30,100,80,30);// setting button position  
	add(b);//adding button into frame  
	setSize(300,300);//frame size 300 width and 300 height  
	setLayout(null);//no layout manager  
	setVisible(true);//now frame will be visible, by default not visible  
}  
	public static void main(String args[]){  
		AWTButton f=new AWTButton ();  
	}
}  

Output:

frameCreation_extend 1

AWT Color Class

The AWT output that we have shown above had default colors for background and foreground. Abstract Window Toolkit provides a Color class that is used to create and set the color to components. We can also set the colors to components using a framework via component properties.

The Color class allows us to the same programmatically. For this purpose, the Color class uses the RGBA color model (RGBA = RED, GREEN, BLUE, ALPHA) or HSB (HSB = HUE, SATURATION, BRIComponents) model.

We will not go into the details of this class, as it is beyond the scope of this tutorial.

The following table lists the various methods provided by the Color class.

Constructor/MethodsDescription
brighter()Create a brighter version of the current color.
createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform x, RenderingHints h)Returns a new PaintContext.
darker()Creates a darker version of the current color.
decode(String nm)Returns a specified opaque color by converting the string to an integer.
equals(Object obj)Checks if the given color object is equal to the current object.
getAlpha()Returns the alpha value of color ranging from 0-255.
getBlue()Returns blue color component in the range 0-255.
getColor(String nm)Returns a color from the system properties.
getColor(String nm, Color v)
getColor(String nm, int v)
getColorComponents(ColorSpace cspace, float[] compArray)Returns an array of type float containing the color components from the specified ColorSpace.
getColorComponents(float[] compArray)Returns an array of type float containing the color components from the ColorSpace of the Color.
getColorSpace()returns the ColorSpace of the current Color.
getGreen()Returns green color component in the range 0-255 in the default sRGB space.
getRed()Returns Red color component in the range 0-255 in the default sRGB space.
getRGB()Returns the RGB value of the current color in the default sRGB ColorModel.
getHSBColor(float h, float s, float b)Creates a Color object using the HSB color model with specified values.
getTransparency()returns the transparency value for this Color.
hashCode()Returns hash code for this Color.
HSBtoRGB(float h, float s, float b)Convert the given HSB to an RGB value
RGBtoHSB(int r, int g, int b, float[] hsbvals)converts the given RGB values to HSB values.

AWT Point In Java

The Point class is used to indicate a location. The location is from a two-dimensional coordinate system.

MethodsDescription
equals(Object)Check if two points are equal.
getLocation()Return location of the current point.
hashCode()Returns the hashcode for the current point.
move(int, int)Moves the given point to the given location in the (x, y) coordinate system.
setLocation(int, int)Changes the point location to the specified location.
setLocation(Point)Sets the location of the point to the given location.
toString()Return the string representation of the point.
translate(int, int)Translate the current point to point at x+dx, y+dy.

AWT Graphics Class

All graphics contexts in Abstract Window Toolkit to draw components in an application derive from the Graphics class. A Graphics class object contains the state information needed to render operations.

The state information typically contains:

  • Which component is to be drawn?
  • Rendering and clipping coordinates.
  • The current color, font, and clip.
  • The current operation on the logical pixel.
  • The current XOR color

The general declaration of the Graphics class is as follows:

public abstract class Graphics

 extends Object

AWT Headless Mode And Headlessexception

When we have a requirement that we should work with the graphics-based application but without an actual keyboard, mouse, or even display, then it is called a “headless” environment.

JVM should be aware of such a headless environment. We can also set the headless environment using Abstract Window Toolkit.

There are certain ways to do this as shown below:

#1) Set the system property “java.awt.headless” to true using the programming code.

#2) Use the command line to set the following headless mode property to true:

java -Djava.awt.headless=true

#3) Add “-Djava.awt.headless=true” to environment variable named “JAVA_OPTS” using a server startup script.

When the environment is headless and we have a code that is dependent on display, the keyboard, or mouse, and when this code is executed in a headless environment then the exception “HeadlessException” is raised.

The general declaration of HeadlessException is given below:

public class HeadlessException

 extends UnsupportedOperationException

We go for the headless mode in applications that require for example image-based image login. For example, if we want to change the image with every login or every time the page is refreshed, then in such cases, we will load the image and we do not require a keyboard, mouse, etc.

Java AWT Vs Swing

Let’s now look at some of the differences between Java AWT and Swing.

AWTSwing
AWT stands for “Abstract Windows Toolkit”. Swing is derived from Java Foundation Classes (JFC).
AWT components are heavyweight as AWT directly makes subroutine calls to subroutines of the Operating System.Swing components are written on top of AWT and as such the components are light-weight.
AWT components are part of the java.awt package.Swing components are part of javax.swing package.
AWT is platform - dependent.Swing components are written in Java and are platform-independent.
AWT doesn’t have its look and feel. It adapts the look and feel of the platform on which it runs.Swing provides a different look and feel of its own.
AWT only has basic features and does not support advanced features like the table, tabbed panel, etc. Swing provides advanced features like JTabbed panel, JTable, etc.
AWT works with 21 peers or widgets of the Operating system that correspond to each component. Swing works with only one peer that is Window Object. All other components are drawn by Swing inside the Window object.
AWT is as good as a thin layer of classes sitting on top of the Operating system which makes it platform-dependent.Swing is larger and also contains rich functionality.
AWT makes us write a lot of things.Swing has most of the features built-in.

Frequently Asked Questions

Q #1) What is AWT in Java?

Answer: AWT in Java also known as “Abstract Window Toolkit” which is a platform-dependent graphical user interface framework that precedes the Swing framework. It is a part of the Java standard GUI API, Java Foundation Classes, or JFC.

Q #2) Is Java AWT still used?

Answer: It is almost obsolete in Java barring a few components that are still used. Also, there are still some old applications or programs running on older platforms that use AWT.

Q #3) What is AWT and Swing in Java?

Answer: Abstract Window toolkit is a platform-dependent API to develop GUI applications in Java. A Swing on the other hand is an API for GUI development and is derived from Java Foundation Classes (JFC). AWT components are heavy-weight while Swing components are light-weight.

Q #4) What is the frame in Java AWT?

Answer: A frame can be defined as the top-level component window that has a title and a border. The Frame has ‘Border layout’ as its default layout. Frames also generate windows events like Close, Opened, Closing, Activate, Deactivated, etc.

Q #5) What is import Java AWT?

Answer: Import Java AWT (import java.awt.*) indicates that we need the functionality of AWT API in our program so that we can use its components like TextFields, Buttons, Labels, List, etc.

Conclusion

In this tutorial, we discussed the overview of Abstract Window Toolkit, as a platform-dependent API for GUI development in Java. It is almost obsolete in Java and is being replaced by the other APIs like Swings and JavaFX.

We have not gone into the details of all the components of Abstract Window Toolkit since they are rarely used now. Hence we only discussed components like Frames, Color, etc. and the headless mode that is set using AWT.

In the next tutorial, we will start with Java Swing tutorials and we will discuss them in detail as most of the Java applications today use Swing for GUI development.

=> Watch Out The Simple Java Training Series Here.