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.
| +-- Applet
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.
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.
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.
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.
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.
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.
A TextField can contain a single row text. It is mostly used for single line data. You already have seen it in lot of username fields and even in the subscribe field in the blog sidebar. It is a sub class of TextComponent
A TextArea can contain multiple rows of text. You might have seen it in Notepad, that's where you write text. As it contains multiple rows and columns, it contains scrollbars.
A checkbox allows user to check or un check an option. Users can check multiple checkboxes. However, you can also use checkbox as a radio button (which restricts user to select only one out of various checkboxes) if several checkboxes are added to a group. All these are covered in the below example.
A Choice allows user to choose an item from a list of similar items. It is a drop down menu also called as a combo box (in swing) and only one item is visible before drop down.
A List unlike a Choice allows user to select multiple items, however there is also an option to restrict to a single selection. But a list looks different, all items are visible in the List.
Working with Menus
A MenuBar is a component that contains menus. A Menu contains MenuItems and other menus. If A menu contains B Menu then B Menu is said to be a sub menu of A Menu.
A CheckboxMenuItem is a MenuItem that can be checked or un-checked. You might have encountered it in Notepad > Format > Wordwrap
A dialog is a window with a title bar but with no minimize or maximize options. It is a direct sub-class of Window and comes with default BorderLayout. You can also remove the title bar as you can in Frame. Also, a dialog can be constructed with a parent, typically a Frame or another dialog.
Dialogs are divided into types, Modal and Modeless (default). A modal dialog blocks user input of top-level windows of the dialog (i.e. its parents, but not its children) while a modeless dialog doesn't. Like the Frame, dialogs can generate WindowEvent.
A FileDialog is a dialog that lets user select a file either for opening or saving. You can see it when you click open/save as in Notepad. It is modal by default, which means that it blocks user input until user selects a file or closes it.
Accessing System tray
You can also access system tray in AWT. You can create an icon and set a popup menu for it.
AWT Toolkit Features
The java.awt.Toolkit class contains methods with which we can do a lot of things to do some basic tweaks.
- Create a Beep sound in Java
- Get all installed fonts in a PC
- Get data from Clipboard in Java
- Get screen size and resolution in Java
Robot class in Java is designed to automate tasks. It contains methods to programatically perform user actions like pressing, releasing keys, clicking mouse button.
Desktop and GraphicsConfiguration classes
Desktop class is used to work with applications installed in Windows. It can be used to open files, url etc. The specialty of this class over Runtime and ProcessBuilder is that, it launches any file in the default application so we don't need to specify. GraphicsConfiguration gives details about output devices like monitor.