Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Java AWT?
2.1.
Why is the Java AWT platform-dependent?
3.
Java AWT Hierarchy
3.1.
Components Of Java AWT 
3.2.
Containers Of Jawa AWT
3.2.1.
Types Of Containers
3.2.2.
Useful Methods of Component Class
4.
Java AWT Example
4.1.
Example Code-1: By Extending the Frame class
4.1.1.
Output
4.2.
Example Code-2: By creating an instance of Frame class
4.2.1.
Output
5.
Frequently Asked Questions
5.1.
What is clipping?
5.2.
What is the use of the window class?
5.3.
How to change a button from enabling to disable after a click?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java AWT Basics

Author Akash Nagpal
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The Abstract Window Toolkit (AWT) is a collection of application software interfaces (APIs) that Java programmers use to construct graphical user interface (GUI) components including buttons, scroll bars, and windows. Sun Microsystems, the company that invented Java, created AWT as part of the Java Foundation Classes (JFC).

This article will overview Java AWT Basics, AWT Hierarchy, its components and containers with the help of various examples.

Also Read About, Multithreading in java

What is Java AWT?

Java AWT, also known as Abstract Window Toolkit, is an API used to create GUI or window-based Java programmes. Since Java AWT components are platform-dependent, they are shown by the operating system's view. It is also heavyweight, suggesting that its components use the Operating System's resources. The Java.awt package contains classes for the AWT API. TextField, CheckBox, Choice, Label, TextArea, Radio Button, List, and so on.

Why is the Java AWT platform-dependent?

Java AWT uses native platform (OS) subroutines to create components such as text boxes, checkboxes, and buttons. An AWT GUI with a button, for example, would have a varied appearance and feel across platforms such as Windows, Mac OS, and Unix since these systems have a different look and feel for their native buttons. AWT directly calls their native subroutine that constructs the button. Simply said, an AWT-based programme will seem like a Windows application when launched on Windows but will look like a Mac application when run on Mac OS.

For its platform dependence behaviour and hefty weight, AWT is rarely used nowadays. Because the underlying operating system creates them, AWT components are considered heavyweight (OS). For example, if you instantiate a text box in AWT, you are essentially asking the operating system to generate a text box.

Read More About, Basics of Java , Why is Java Platform Independent

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Java AWT Hierarchy

The hierarchy of Java AWT classes is given below:

 


Image Source: WebEduClick.com

 

Components Of Java AWT 

Components include all elements such as buttons, text fields, scrollbars, and so on. As seen in the picture above, AWT has classes for each component. To have everything on a screen in a specific position, we must add them to a container. A container is similar to a screen on which we place components such as buttons, text fields, checkboxes, etc. In a nutshell, a container holds and controls the arrangement of components. Because a container is a component (as seen in the above hierarchy diagram), we may put a container within a container.

Containers Of Jawa AWT

The Container is an AWT component that may hold other components like buttons, text fields, labels, etc. Containers are classes that extend the Container class, such as Frame, Dialog, and Panel. It is simply a screen where the components are positioned in their respective areas. As a result, it includes and governs the arrangement of components.

Types Of Containers

  • Window: A Window class instance has no border and no title.
  • Dialog: The dialog class has a border and a title. A Dialog class instance cannot exist without a Frame class instance.
  • Panel: There is no title bar, menu bar, or border in the panel. It is a generic container used to store components. A Panel class instance serves as a container to which components can be added.
  • Frame: A-frame has a title, a border, and menu bars. It may include various components such as buttons, text fields, scrollbars, etc. This is the most often used Container when constructing an AWT application.

Useful Methods of Component Class

Java AWT Example

We may use Frame to design a GUI in two ways:

1) By Extending the Frame class.

2) By making an instance of the Frame class.

Example Code-1: By Extending the Frame class

import java.awt.*;
// Extended Frame class 
// Theclass "SimpleExample" will 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


Example Code-2: By creating an 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

Try it by yourself on java online compiler.

Frequently Asked Questions

What is clipping?

Clipping is limiting paint activities to a certain region or form. It is widely used in GUI and Graphic designing.

What is the use of the window class?

The window class may be used to build a simple, bare-bones window without a border or menu. The window class may also be used to display welcome or introduction screens.

How to change a button from enabling to disable after a click?

When a button is pressed, an action event is triggered, which may be recorded by implementing the ActionListener interface and the actionPerformed(ActionEvent ae) function. You can then press the button. To deactivate this button, use setEnable(false).

Conclusion

In this article, we have extensively discussed Java AWT Basics, and its hierarchy with the help of various examples. We have also brushed up on some of the important java Concepts.  We hope this blog has helped you enhance your knowledge regarding the C++ concepts. Some additional documentation guides on Java AWT that can help you improve your understanding are Java AWT Button and Java String compareto().

If you would like to learn more, check out our articles on Operator Keyword in C#cloud platform comparison, and 10 AWS best books. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Next article
Java AWT Button
Live masterclass