Here is an exhaustive
AWT tutorial in Java for beginners. This tutorial covers introduction to the AWT, its components, advantages and drawbacks. You can find the
Java Event Handling tutorial here.
Introduction to the AWT
AWT (a.k.a Abstract Window Toolkit) is a concept designed for building simple GUIs in Java applications (both applets and desktop). AWT is introduced in the year 1995 when Java was first introduced. It comes with a thin layer of components with a small learning curve when compared to that of the Swing. As creating GUI components need, operating system support, AWT components are developed in native code. This is the reason why AWT components look different on different operating systems thereby ruining the Java paradigm
Write once, run anywhere. The code runs anywhere, but not in the desired way. Just like Swing, AWT is not thread safe meaning, it doesn't run effectively (or in the desired way) on other threads. A separate thread called the
Event dispatch thread runs the GUI part while other background processes are carried out by other threads such as the main thread for better performance.
Why do we need GUI over CUI?
Before we get into the AWT, it is important to know the advantages of building GUI applications over CUI apps.
1. GUI applications are user-friendly, they don't require user to have knowledge of commands (like in the DOS).
2. GUI applications support multi tasking which means that the users can perform various tasks (in other words, work with several apps) at a time. For example, he can listen to music in foobar and play an NFS game. In a CUI, you need a command to complete before executing another command.
3. In CUI you can only see text, where as in GUIs you can see images, videos and flash.
Examples of GUI include Windows XP, Mac OS X, Ubuntu and those of CUI include DOS etc.
AWT Class hierarchy
The
ultimate super class of all the GUI components both in AWT as well as Swing is the
java.awt.Component. An immediate sub-class of it is the
Container class. So every container is a component (from inheritance). Before learning the hierarchy, let us see what is a component and a container.
A component refers to a GUI control such as Button, Label etc.
A container refers to a GUI control that is capable of containing other GUI controls in it. Typical examples include a Frame, Panel, Dialog etc.
Component
|
+-- Container
|
+-- Panel
| |
| +-- Applet
|
+-- Window
|
+-- Frame
Component class and its sub classes
Component class is an immediate sub class of the Object. The other immediate sub classes include Label, Button, Checkbox, Choice, List, Canvas, Scrollbar and TextComponent.
Window class
A window in AWT is a container represents a top-level window that doesn't have a title bar (so it doesn't have close, minimize, maximize buttons), menu bar. This sits on the desktop directly and has a BorderLayout by default.
Frame class
It is a sub class of Window class which has a title bar, menu bar, borders and is re-sizable with border layout by default. This is what we use in our programs to create windows.
- Creating an AWT Frame in the easy way
Panel class
It is a sub class of the Container class. It is a border less window that doesn't contain any title bar or menubar. usually added to another container like the Frame. The main purpose of a panel is to group components. It comes with a default FlowLayout.
- Creating an AWT Panel in Java
Label class
A label is an informatory GUI component that typically describes another component. For example, you see a label Username side of a username field to tell you that the textfield is for typing username.
- Creating Labels in AWT
A Button is the most common GUI control that lets you click on it do some action. You might already have seen it a lot of times, in OK and Cancel buttons, for example.