0% found this document useful (0 votes)
100 views7 pages

AWT Hierarchy

The document discusses Java AWT (Abstract Window Toolkit), which is an API for developing GUI applications in Java. It provides classes like TextField, Label, TextArea etc. that allow creating GUI components. AWT components are platform-dependent and heavyweight. Containers like Frame, Dialog and Panel are used to hold and arrange components. The document also provides examples of creating Frame windows by extending the Frame class and by creating Frame instances.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views7 pages

AWT Hierarchy

The document discusses Java AWT (Abstract Window Toolkit), which is an API for developing GUI applications in Java. It provides classes like TextField, Label, TextArea etc. that allow creating GUI components. AWT components are platform-dependent and heavyweight. Containers like Frame, Dialog and Panel are used to hold and arrange components. The document also provides examples of creating Frame windows by extending the Frame class and by creating Frame instances.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

AWT

• Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in
java.
• Java AWT components are platform-dependent i.e. components are displayed according to the view
of operating system. AWT is heavyweight i.e. its components are using the resources of OS.
• The java.awt package provides classes for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
• AWT is rarely used now days because of its platform dependent and heavy-weight nature. Swing is
a preferred API for window based applications because of its platform independent and light-weight
nature.

AWT hierarchy

Components and containers


All the elements like buttons, text fields, scrollbars etc are known as components. In AWT we have classes
for each component as shown in the above diagram. To have everything placed on a screen to a particular
position, we have to add them to a container. A container is like a screen wherein we are placing
components like buttons, text fields, checkbox etc. In short a container contains and controls the layout of
components. A container itself is a component (shown in the above hierarchy diagram) thus we can add a
container inside container.

Types of containers:
As explained above, a container is a place wherein we add components like text field, button, checkbox etc.
There are four types of containers available in AWT: Window, Frame, Dialog and Panel. As shown in the
hierarchy diagram above, Frame and Dialog are subclasses of Window class.

Window: An instance of the Window class has no border and no title


Dialog: Dialog class has border and title. An instance of the Dialog class cannot exist without an associated
instance of the Frame class.
Panel: Panel does not contain title bar, menu bar or border. It is a generic container for holding
components. An instance of the Panel class provides a container to which to add components.
Frame: A frame has title, border and menu bars. It can contain several components like buttons, text fields,
scrollbars etc. This is most widely used container while developing an application in AWT.

Useful Methods of Component class

Method Description

public void add(Component c) Inserts a component on this component.

public void setSize(int width,int height) Sets the size (width and height) of the component.

public void setLayout(LayoutManager m) Defines the layout manager for the component.

public void setVisible(boolean status) Changes the visibility of the component, by default false.

AWT Classes

The AWT classes are contained in the java.awt package. It is one of Java’s largest packages.
Fortunately, because it is logically organized in a top-down, hierarchical fashion, it is easier
to understand and use .
Class Description

AWTEvent Encapsulates AWT events.


AWTEventMulticaster Dispatches events to multiple listeners.
BorderLayout The border layout manager. Border layouts use five components:
North, South, East, West, and Center.
Button Creates a push button control.
Canvas A blank, semantics-free window.
CardLayout The card layout manager. Card layouts emulate index cards.
Only the one on top is showing.
Checkbox Creates a check box control.
CheckboxGroup Creates a group of check box controls.
CheckboxMenuItem Creates an on/off menu item.
Choice Creates a pop-up list.
Color Manages colors in a portable, platform-independent fashion.
Component An abstract superclass for various AWT components.
Container A subclass of Component that can hold other components.
Cursor Encapsulates a bitmapped cursor.
Dialog Creates a dialog window.
Dimension Specifies the dimensions of an object. The width is stored in width,
and the height is stored in height.
Event Encapsulates events.
EventQueue Queues events.
FileDialog Creates a window from which a file can be selected.
FlowLayout The flow layout manager. Flow layout positions components left
to right, top to bottom.
Font Encapsulates a type font.
FontMetrics Encapsulates various information related to a font. This information
helps you display text in a window.
Frame Creates a standard window that has a title bar, resize corners, and
a menu bar.
NOTE:
There are several key methods you will use when working with Frame windows. They
are examined here.
Setting the Window’s Dimensions
The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
The new size of the window is specified by newWidth and newHeight, or by the width and
height fields of the Dimension object passed in newSize. The dimensions are specified in
terms of pixels.
The getSize( ) method is used to obtain the current size of a window. Its signature is
shown here:
Dimension getSize( )
This method returns the current size of the window contained within the width and height
fields of a Dimension object.
Hiding and Showing a Window
After a frame window has been created, it will not be visible until you call setVisible( ).
Its signature is shown here:
void setVisible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.
Setting a Window’s Title
You can change the title in a frame window using setTitle( ), which has this general form:
void setTitle(String newTitle)
Here, newTitle is the new title for the window.
Closing a Frame Window
When using a frame window, your program must remove that window from the screen when
it is closed, by calling setVisible(false). To intercept a window-close event, you must implement
the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ),
you must remove the window from the screen. The example in the next section illustrates
this technique.

Java AWT Example


We can create a GUI using Frame in two ways:
1) By extending Frame class
2) By creating the instance of Frame class

AWT Example 1: creating Frame by extending Frame


class
import java.awt.*;
/* We have extended the Frame class here,
* thus our class "SimpleExample" would behave
* like a Frame
*/
public class SimpleExample extends Frame{
SimpleExample(){
Button b=new Button("Button!!");

// setting button position on screen


b.setBounds(50,50,50,50);

//adding button into frame


add(b);

//Setting Frame width and height


setSize(500,300);

//Setting the title of Frame


setTitle("This is my First AWT example");

//Setting the layout for the Frame


setLayout(new FlowLayout());

/* By default frame is not visible so


* we are setting the visibility to true
* to make it visible.
*/
setVisible(true);
}
public static void main(String args[]){
// Creating the instance of Frame
SimpleExample fr=new SimpleExample();
}
}
Output:
AWT Example 2: creating Frame by creating instance of
Frame class
import java.awt.*;
public class Example2 {
Example2()
{
//Creating Frame
Frame fr=new Frame();

//Creating a label
Label lb = new Label("UserId: ");

//adding label to the frame


fr.add(lb);

//Creating Text Field


TextField t = new TextField();

//adding text field to the frame


fr.add(t);

//setting frame size


fr.setSize(500, 300);

//Setting the layout for the Frame


fr.setLayout(new FlowLayout());

fr.setVisible(true);
}
public static void main(String args[])
{
Example2 ex = new Example2();
}
}

Output:

You might also like