
The Abstract Window Toolkit (AWT) is
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
's original platform-dependent
windowing,
graphic
Graphics () are visual images or designs on some surface, such as a wall, canvas, screen, paper, or stone, to inform, illustrate, or entertain. In contemporary usage, it includes a pictorial representation of the data, as in design and manufa ...
s, and
user-interface
In the industrial design field of human–computer interaction, a user interface (UI) is the space where interactions between humans and machines occur. The goal of this interaction is to allow effective operation and control of the machine fro ...
widget toolkit
A widget toolkit, widget library, GUI toolkit, or UX library is a library (computing), library or a collection of libraries containing a set of graphical control elements (called ''widgets'') used to construct the graphical user interface (GUI) of ...
, preceding
Swing. The AWT is part of the
Java Foundation Classes (JFC) — the standard
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
for providing a
graphical user interface
A graphical user interface, or GUI, is a form of user interface that allows user (computing), users to human–computer interaction, interact with electronic devices through Graphics, graphical icon (computing), icons and visual indicators such ...
(GUI) for a Java program. AWT is also the GUI toolkit for a number of
Java ME
Java Platform, Micro Edition or Java ME is a computing platform for development and deployment of porting, portable code for embedded system, embedded and mobile devices (micro-controllers, sensors, gateways, mobile phones, personal digita ...
profiles. For example,
Connected Device Configuration profiles require Java
runtimes on
mobile telephones to support the Abstract Window Toolkit.
History
When
Sun Microsystems
Sun Microsystems, Inc., often known as Sun for short, was an American technology company that existed from 1982 to 2010 which developed and sold computers, computer components, software, and information technology services. Sun contributed sig ...
first released Java in 1995, AWT widgets provided a thin level of abstraction over the underlying native user-interface. For example, creating an AWT
check box would cause AWT directly to call the underlying native subroutine that created a check box. However, the check box on
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
is not the same as the check box on
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
or on the various types of
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
. Some application developers prefer this model because it provides a high degree of fidelity to the underlying native windowing toolkit and seamless integration with native applications. In other words, a GUI program written using AWT looks like a native Microsoft Windows application when run on Windows, but the same program looks like a native
Apple Macintosh
Mac is a brand of personal computers designed and marketed by Apple Inc., Apple since 1984. The name is short for Macintosh (its official name until 1999), a reference to the McIntosh (apple), McIntosh apple. The current product lineup inclu ...
application when run on a Mac, etc. However, some application developers dislike this model because they prefer their applications to look exactly the same on every platform.
In
J2SE 1.2, the
Swing toolkit largely superseded the AWT's widgets. In addition to providing a richer set of UI widgets, Swing draws its own widgets (by using
Java 2D to call into low-level subroutines in the local graphics subsystem) instead of relying on the operating system's high-level user interface module. Swing provides the option of using either the native platform's
"look and feel" or a cross-platform look and feel (the "Java Look and Feel") that looks the same on all windowing systems.
Architecture
The AWT provides two levels of
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
s:
* A general interface between Java and the native system, used for
windowing,
events, and
layout manager Layout managers are software components used in widget toolkits which have the ability to lay out graphical control elements by their relative positions without using distance units. It is often more natural to define component layouts in this mann ...
s. This API is at the core of Java
GUI programming and is also used by
Swing and
Java 2D. It contains:
** The interface between the native windowing system and the Java application;
** The core of the GUI
event subsystem;
** Several
layout manager Layout managers are software components used in widget toolkits which have the ability to lay out graphical control elements by their relative positions without using distance units. It is often more natural to define component layouts in this mann ...
s;
** The interface to
input device
In computing, an input device is a piece of equipment used to provide data and control signals to an information processing system, such as a computer or information appliance. Examples of input devices include keyboards, computer mice, scanne ...
s such as
mouse
A mouse (: mice) is a small rodent. Characteristically, mice are known to have a pointed snout, small rounded ears, a body-length scaly tail, and a high breeding rate. The best known mouse species is the common house mouse (''Mus musculus'' ...
and
keyboard; and
** A
package for use with the
Clipboard and
Drag and Drop
In computer graphical user interfaces, drag and drop is a pointing device gesture in which the user (computing), user selects a virtual object by "grabbing" it and dragging it to a different location or onto another virtual object. In general, i ...
.
* A basic set of GUI widgets such as buttons, text boxes, and menus. It also provides the
AWT Native Interface, which enables
rendering libraries
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
compiled to
native code
In computer programming, machine code is computer program, computer code consisting of machine language instruction set architecture, instructions, which are used to control a computer's central processing unit (CPU). For conventional binary ...
to draw directly to an AWT
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an a ...
drawing surface.
AWT also makes some higher level functionality available to applications, such as:
* Access to the
system tray
The taskbar is a graphical user interface element that has been part of Microsoft Windows since Windows 95, displaying and facilitating switching between running computer program, programs. The taskbar and the associated Start menu, Start Menu were ...
on supporting systems; and
* The ability to launch some desktop applications such as
web browser
A web browser, often shortened to browser, is an application for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's scr ...
s and
email clients from a Java application.
Neither AWT nor Swing is inherently
thread safe
In multi-threaded computer programming, a function is thread-safe when it can be invoked or accessed concurrently by multiple threads without causing unexpected behavior, race conditions, or data corruption. As in the multi-threaded context where ...
. Therefore, code that updates the GUI or processes events should execute on the
Event dispatching thread. Failure to do so may result in a
deadlock or race condition. To address this problem, a utility class called
SwingWorker allows applications to perform time-consuming tasks following user-interaction events in the event dispatching thread.
Mixing AWT and Swing components
Where there is a Swing version of an AWT component it will begin with J- and should be used exclusively, replacing the AWT version. For example, in Swing, only use JButton, never Button class. As mentioned above, the AWT core classes, such as Color and Font, are still used as-is in Swing.
When drawing in Swing, use JPanel and override paintComponent(Graphics g) instead of using the AWT paint() methods.
Before
Java 6 Update 12, mixing
Swing components and basic AWT widgets often resulted in undesired side effects, with AWT widgets appearing on top of the Swing widgets regardless of their defined
z-order. This problem was because the rendering architecture of the two widget toolkits was very different, despite Swing borrowing heavyweight top
containers
A container is any receptacle or enclosure for holding a product used in storage, packaging, and transportation, including shipping.
Things kept inside of a container are protected on several sides by being inside of its structure. The term ...
from AWT.
Starting in
Java 6 Update 12, it is possible to mix Swing and AWT widgets without having z-order problems.
Example
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyApp
Implementation
As the AWT is a bridge to the underlying native user-interface, its implementation on a new
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
may involve a lot of work, especially if it involves any of the AWT GUI widgets, because each of them requires that its native peers be developed from scratch.
A new project
Caciocavallo has been created, that provides an
OpenJDK
OpenJDK (Open Java Development Kit) is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It is the result of an effort Sun Microsystems began in 2006, four years before the company was acquired by Oracle Corp ...
-based Java API to ease AWT implementation on new systems. The project has successfully implemented AWT widgets using
Java2D. All the necessary core-JDK modifications have since been pushed to
OpenJDK 7, which means that Java can now be used on a graphics stack other than one of those provided by the official JDK (
X Window System
The X Window System (X11, or simply X) is a windowing system for bitmap displays, common on Unix-like operating systems.
X originated as part of Project Athena at Massachusetts Institute of Technology (MIT) in 1984. The X protocol has been at ...
,
OpenGL
OpenGL (Open Graphics Library) is a Language-independent specification, cross-language, cross-platform application programming interface (API) for rendering 2D computer graphics, 2D and 3D computer graphics, 3D vector graphics. The API is typic ...
or
DirectX
Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with "Direct" ...
), by including an external library and setting some system properties. A
DirectFB backend for Caciocavallo
is under development, as is an
HTML5
HTML5 (Hypertext Markup Language 5) is a markup language used for structuring and presenting hypertext documents on the World Wide Web. It was the fifth and final major HTML version that is now a retired World Wide Web Consortium (W3C) recommend ...
backend; the aim is to deploy existing Swing applications—without Java support—as ordinary web applications running on a web server.
See also
*
Swing (Java)
Swing is a Graphical user interface, GUI widget toolkit for Java (programming language), Java. It is part of Oracle Corporation, Oracle's Java Foundation Classes (JFC) – an Application programming interface, API for providing a graphi ...
*
Standard Widget Toolkit
The Standard Widget Toolkit (SWT) is a graphical widget toolkit for use with the Java platform. It was originally developed by Stephen Northover at IBM and is now maintained by the Eclipse Foundation in tandem with the Eclipse IDE. It is an alte ...
References
External links
* (AWT
Javadoc
Javadoc (also capitalized as JavaDoc or javadoc) is an API documentation generator for the Java programming language. Based on information in Java source code, Javadoc generates documentation formatted as HTML and other formats via extensions. ...
API documentation)
*
AWT/Swing
{{Widget toolkits
Articles with example Java code
JDK components
Widget toolkits