Using Abstract Windowing Toolkit (AWT) In Applications

A well laid out user interface is an effective means of making applications user-friendly. It is typically used by organizations for accepting orders from customers, getting customer’s feedback on a product, for conducting a market survey on the latest trends in economy.

A well laid out user interface is an effective means of making applications user-friendly. It is typically used by organizations for accepting orders from customers, getting customer’s feedback on a product, for conducting a market survey on the latest trends in economy. It finds its use even in stock market, political polling, conducting a test over the Internet using forms, the list seems never ending. User interfaces are of two types, Character User Interface (CUI) and Graphical User Interface (GUI).

Using CUI, you can interact with the system by keying in commands. Classic example of CUI is MS-DOS, the only disadvantage is that you have to remember all the commands and syntax. Today, GUI has been accepted as a worldwide standard for software applications. Since they provide a “picture-oriented” or “graphical” approach for interacting with the system. They are not only easy to use by also to learn. You don’t have to remember long lines of syntax, because most of the commands are executed by a simple mouse click. Microsoft’s Windows family of products is an example of an operating system that supports GUI.

In the present world with the popularity of Apple’s Macintosh and Microsoft Windows operating system, most of today’s computer users expect their software to feature a graphical user interface and things they can control with a mouse. Typically GUI includes windows, menu, buttons, scroll bars, icon images and wizards. Almost all Operating systems today provide GUI plus add their own elements. An integral part of any programming language is the ability to accept data from a use. The user has to be prompted for data entry. To simplify user interaction and make data entry easier, you can use controls. Controls are components like buttons and text boxes that can be added to containers like frame, panels and applets. The Java AWT package provides an integrated set of classes to manage user interface components {mospagebreak title=Read On….Its Not That Abstract} Abstract Windowing Toolkit:
The Abstract Windowing Toolkit (AWT) is a package that provides an integrated set of classes that enable you to create a graphical user interface and receive user input from mouse and keyboard. Because Java is a platform–independent language, the AWT offers a way to design an interface that will have the same general appearance and functionality on all systems it runs on.

Using the AWT, a user interface consists of three things:

•Components: Anything that can be put onto user interface including components like windows, buttons, check boxes, dialog boxes, lists, menus, scrollbars, and text fields. The Component class, which implements the common functionality, is the super class for all graphical interface elements.

•Container: A component that can contain other components. A classic example is the Applet window and others include panel, dialog boxes, standalone windows.

•Layout Manager: An object that defines how the components in a container will be arranged. You don’t see the Layout Manager in an interface, but you definitely see the results of its work on the look and feel of your GUI.

The AWT’s classes are all part of the java.awt package. To make all of its classes available in a program, the following statement can be used at the top of a source code file. Import java.awt.*;
This imports the entire components, containers and layout manager that you will use to design an interface. You can also use individual import statements with the classes you are using in a program. The AWT’s classes like all parts of the Java class library are arranged into inheritance hierarchy. {mospagebreak title=Your Best Buddies….Java’s AWT} User-Interface Components: The methods of the Component class allows you to control your on-screen appearance and internal state of all components. The following diagram depicts the component hierarchy in Java’s AWT.

{mospagebreak title=Features Of The Component Class} All components are implemented as subclass of the Component class. The subclasses inherit a substantial amount of functionality from the Component class. The basic features of the Component class are:

Drawing Support:
The component class provides paint (), update() and repaint() methods to help us draw the components on the screen.

A component is capable of delegating its event (like button click, mouse move) to other classes for processing. We will explore event handling in detail in the later part of the tutorial.

Apperance Control:
The component class provides methods for getting information about the current font, foreground and background color. Custom drawing of components is also made possible because of this.

The component class provides an implementation of the ImageObserver interface and defines methods to help components display images.

Sizing And Positioning Of Controls:
The size and position of all components are subjected to the requirement specified by the layout manager. Although each component can somewhat determine its own size and position, the component class provides methods to resize and position components as preferred by the programmer.

Now that we have laid the foundation about Java AWT, we will be examining each of them in-depth in the subsequent parts of this tutorial.{mospagebreak title=What Holds Components Together…The Container} Containers:
Some components can also act as containers. A window for example, can be a part of another container. At the same time it can be a parent to other components, such as textbox, and a checkbox, it is a container as well. The container class is a specialized subclass of the Component class. A container may contain zero or more components. These components are called siblings, since they have the same parent window. There are three main types of containers-Window, Applet and Panel. There are two types of windows-Frame and Dialog. A frame is a rectangular box with a title and resize button. A dialog box is similar to a frame except that it cannot be resized and does not have a menu bar.

The Frame Class:
A frame is a powerful feature of AWT. You can create a window for your application using Frame class. A frame has a title bar, resizable border and an optional menu bar. You can add components to the frame using add () method as frames are derived form java.awt.container. The Border layout is the default layout of the frame. A frame receives mouse events, keyboard events and focus events.

The constructor of the Frame class receives the title of the frame as parameter. The string or name you specify will be displayed as the title of the frame.

Frame f = new Frame (“Frame Window Demo”);
After the window is created, it can be displayed by calling the setVisible () method and can be sized by calling the setSize () method. The program below displays a frame.
Import java.awt.*; Public class frame extends Frame { Public static void main (String arg[]) { Frame frm; frm = new Frame(“My First AWT Container-The Frame”); frm.setSize(300,400); frm.setBackground (; frm.setVisible(true) } }

Save the above program as, compile using Javac and execute using Java file name. The output will be a frame window with the title “My Frist AWT Container-The Frame”. {mospagebreak title=How To Talk To Your Application} The Dialog Class:

Dialogs are pop-up windows that are used to accept input from a user. There are two kinds of dialog boxes-modal and modeless. Unlike a modeless dialog box, a modal dialog box does not allow a user to interact with other window while it is displayed. A frame or another dialog must own a dialog box. You cannot directly attached a dialog to an applet. You can attach a dialog to a frame.

When you create a dialog box, you can specify whether you want a modal or modeless dialog box. You cannot change the “modality” of the dialog box after creating it. We use Dialog class for creating dialog boxes. The dialog class offers overloaded constructors.

The following constructors create a dialog box that has a frame as its owner:

    •Dialog (Frame owner_frame)
    •Dialog (Frame owner_frame, String title).
    •Dialog (Frame owner_frame, String title, Boolean modal).
The following constructors create a dialog box that has another dialog box as its owner:

    •Dialog(Dialog owner_dialog)
    •Dialog (Dialog owner_dialog, String title).
    •Dialog (Dialog owner_dialog, String title, Boolean modal).

The single argument constructor creates a modeless dialog box without a title. The second argument that you could pass to a constructor is the title of the dialog box. To create a modal dialog box, you could use the third argument. An example is given below.
Dialog dialog = new Dialog (frame, “Demo Of Dialog”, true);
Getting To Know About Panels The Panel Class: Panels are used for organizing components. You can use the Layout Manager to decide how the components are to be arranged on the panel. Each panel can have a different layout. To create a panel, use the following command:
Panel panel = new Panel ();
Sub-Panels: You can also create what is called the “nested panels”, with one panel containing more than one sub-panels. You can nest panels, as many levels up as you like. Once you have created a component, the simplest way to add it to the container is to call the container’s add () method. For example:
Panel Mainpanel, Subpanel1, Subpanel2; Mainpanel = new Panel (); Subpanel1 = new Panel (); Subpanel2 = new Panel (); Mainpanel.add(Subpanel1); Mainpanel.add(Subpanel2);
So we have covered some basics about Java AWT, discussed at length about the Component class. Enumerated on the first component-the container and its various types like windows, Frame, Panel and Dialog boxes. In the next part of the tutorial we will see how to simplify user interaction and make data entry easier, with the use of use controls. Controls are components like buttons and text box, check box, radio button etc. that can be added to containers like frame, panels and applets. We will also cover additional information about Java AWT package which provide an integrated set of classes to manage user interface components and then write a few codes to test our knowledge.

Google+ Comments

Google+ Comments