Updated March 14, 2023
Introduction to AWT in Java
Java AWT is also known as Abstract Window Toolkit is an API that is used to develop either GUI or window-based applications in Java. Java AWT components are platform-dependent which implies that they are displayed according to the view of the operating system. It is also heavyweight implying that its components are using the resources of the Operating System. java. awt package provides classes for AWT api. For example, TextField, CheckBox, Choice, Label, TextArea, Radio Button, List, etc.
AWT hierarchy
Following is AWT hierarchy:
Container
The Container is one of the components in AWT that contains other components like buttons, text fields, labels, etc. The classes that extend the Container class are known as containers such as Frame, Dialog, and Panel as shown in the hierarchy.
Types of containers
As demonstrated above, container refers to the location where components can be added like text field, button, checkbox, etc. There are in total, four types of containers available in AW, that is, Window, Frame, Dialog, and Panel. As shown in the hierarchy above, Frame and Dialog are subclasses of the Window class.
1. Window: The window is a container that does not have borders and menu bars. In order to create a window, you can use frame, dialog or another window.
2. Panel: The Panel is the container/class that doesn’t contain the title bar and menu bars. It has other components like buttons, text fields, etc.
3. Dialog: The Dialog is the container or class having a border and title. We cannot create an instance of the Dialog class without an associated instance of the respective Frame class.
4. Trim: The Frame is the container or class containing the title bar and might also have menu bars. It can also have other components like text field, button, etc.
Why AWT is platform dependent?
Java Abstract Window Toolkit calls native platform I.e., Operating system’s subroutine in order to create components like text box, checkbox, button, etc. For example, an AWT GUI containing a button would be having varied look- and -feel in various platforms like Windows, Mac OS, and Unix, etc. since these platforms have different look and feel for their respective native buttons and then AWT would directly call their native subroutine that is going to create the button. In simple words, an application build on AWT would look more like a windows application when being run on Windows, however, that same application would look like a Mac application when being run on Mac Operating System.
Basic Methods of Component Class
- public void add(Component c): This method would insert a component on this component.
- public void setSize(int width, int height): This method would set the size (width and height ) of the particular component.
- public void setVisible(boolean status): This method would change the visibility of the component, which is by default false.
- public void setLayout(LayoutManager m): This method would define the layout manager for the particular component.
Java AWT Example
We can create a GUI using Frame in two ways:
Either by extending Frame class or by creating the instance of Frame class
Let’s show this by both examples, first extending Frame Class :
import java.awt.*;/* Extend the Frame class here,
*thus our class "Example" would behave
*like a Frame
*/public class Example extends Frame
{Example()
{Button b=new Button("Button!!");
//setting button position on screen
b.setBounds(50,50,50,50);
//adding button into frame
add(b);
//Setting width and height
setSize(500,300);
//Setting title of Frame
setTitle("This is 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
Example fr=new Example();
}
}
Example:
import java.awt.*;
public class Example {
Example()
{
//Creating Frame
Frame f=new Frame();
//Creating a label
Label l = new Label("User: ");
//adding label to the frame
f.add(l);
//Creating Text Field
TextField t = new TextField();
//adding text field to the frame
f.add(t);
//setting frame size
f.setSize(500, 300);
//Setting the layout for the Frame
f.setLayout(new FlowLayout());
f.setVisible(true);
}
public static void main(String args[])
{Example ex = new Example();
}
}
Layouts in AWT
There are 2 layouts in AWT which are as follows :
Flow layout is the default layout, which implies when you don’t set any layout in your code then the particular layout would be set to Flow by default. Flow layout would put components like text fields, buttons, labels, etc in a row form and if horizontal space is not long enough to hold all components then it would add them in the next row and cycle goes on. Few points about Flow Layout
- All the rows in Flow layout are aligned center by default. But, if required we can set the alignment from left or right.
- The horizontal and vertical gap between all components is 5 pixels by default.
- By default, the orientation of the components is left to right, which implies that the components would be added from left to right as required, but we can change it from right to left when needed.
Border layout wherein we can add components like text fields, buttons, labels, etc to specific five These regions are known as PAGE_START, LINE_START, CENTER, LINE_END, PAGE_END.
Method for border layout is:
public BorderLayout(int hgap,int vgap)
It would construct a border layout with the gaps specified between components. The horizontal gap is specified by hgap and the vertical gap is specified by vgap.
Parameters are :
- hgap: The horizontal gap.
- vgap: The vertical gap.
We can also achieve the same by using setHgap(int hgap) method for the horizontal gap between components and setVgap(int vgap) method for the vertical gap.
Conclusion
Therefore, we can conclude that AWT was designed in order to provide a common set of tools for GUI design that could work on a variety of platforms. These tools preserved the look and feel of each platform.
Recommended Articles
This has been a guide to What is AWT in Java. Here we have discuss the hierarchy, basic method, example and layouts of AWT in Java. You may also look at the following articles to learn more –