ADS

Java Awt Tutorial For Beginners

 This tutorial covers introduction to the AWT Java AWT Tutorial for Beginners
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 kafe (so it doesn't have close, minimize, maximize buttons), sajian 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, sajian bar, borders and is re-sizable with border layout by default. This is what we use in our programs to create windows.
  1. 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 kafe 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.
  1. 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.
  1. Creating Labels in AWT

Button class

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.
  1. Creating an AWT Button in Java

TextField class

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
  1. 4 Ways to Create an AWT TextField

TextArea class

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.
  1. AWT TextArea in Java - All methods and constructors

Checkbox class

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.
  1. 5 Ways to Create an AWT Checkbox

Choice class

A Choice allows user to choose an item from a list of similar items. It is a drop down sajian also called as a combo box (in swing) and only one item is visible before drop down.
  1. Creating an AWT Choice in Java

List class

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.
  1. Create an AWT List in Java

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 sajian of A Menu.
  1. Creating Menus and MenuItems in AWT Example

CheckboxMenuItem class

A CheckboxMenuItem is a MenuItem that can be checked or un-checked. You might have encountered it in Notepad > Format > Wordwrap
  1. CheckboxMenuItem in AWT Example

Dialog class

A obrolan is a window with a title kafe 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 kafe as you can in Frame. Also, a obrolan can be constructed with a parent, typically a Frame or another dialog.
Dialogs are divided into types, Modal and Modeless (default). A modal obrolan blocks user input of top-level windows of the obrolan (i.e. its parents, but not its children) while a modeless obrolan doesn't. Like the Frame, dialogs can generate WindowEvent.
  1. AWT Dialog in Java Example

FileDialog class

A FileDialog is a obrolan 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.
  1. Example on AWT FileDialog

Accessing System tray

You can also access system tray in AWT. You can create an icon and set a popup sajian for it.
  1. Access system tray in AWT

AWT Toolkit Features

The java.awt.Toolkit class contains methods with which we can do a lot of things to do some basic tweaks.
  1. Create a Beep sound in Java 
  2. Get all installed fonts in a PC 
  3. Get data from Clipboard in Java 
  4. Get screen size and resolution in Java 

Robot class

Robot class in Java is designed to automate tasks. It contains methods to programatically perform user actions like pressing, releasing keys, clicking mouse button.
  1. How to take a screenshot using Robot?
  2. Using Robot class in Java AWT

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.
  1. Open a URL in Java
  2. Get screen center point in Java
Thanks for reading the post. This post will be updated with a lot of new examples on different topics in the Java AWT. So stay tuned!

Subscribe to receive free email updates:

ADS