Breaking

Java AWT (Abstract Window Toolkit)


                    AWT (Abstract Window Toolkit)



Working with Graphical User Interface  (GUI) is always a fascinating task to the end users as well as interface designers. GUI based application provide click and execute environmental that make applications more entertaining and interesting. Java provides a rich API, called Abstract Window Toolkit (AWT), to create various components.
                However, a GUI designed on one platform may look different when displayed on another platform.
               It is inherently challenging to design and implement effective GUIs. Moreover, complicated interaction between classes in the AWT sometimes makes this task more complex. However, with proper guidance, the creation of GUIs is not only possible but relatively straightforward.

AWT Class Hierarchy:-

Java allows programmers to create various graphical components such as a window, frame, panel, button, text field, text area, radio button, checkbox, drop-down list, etc.
       In AWT, only 2 package – java.awt and java. awt. event - are commonly used.

Java AWT Hierarchy:-

·      AWT Component:-  AWT components are graphic elements that can be displayed on the screen and that can interact with the user. For example, button, text boxes, checkboxes, radio button are all examples of components which are represented by classes Button,   TextField, Checkbox, CheckboxGroup respectively.  All component classes are direct or indirect subclasses of the abstract class java. awt component. AWT provides component’s basic behavior, such as adding/removing listeners, getting/setting current coordinates, showing/hiding them etc.

·      AWT Container:-

Some components (such as a button, text boxes) cannot stand alone and require some other special component
to contain them. These special components are called containers. Examples of such containers are, window, panel, scroll pane, etc, which are represented by Window, Panel, and ScrollPane classes respectively. The super-class of all these container classes is container class, which defines container’s common behavior such as adding/removing/getting components, choosing a layout, etc. Container's of components added to it using a list. 

1.   Window- A AWT window container is a top level display surface without any borders and menu bar. It must contain either a frame, dialog, which can directly contain other components. A Window object cannot be attached to nor be embedded with in a different kind of container. 

2.   Frame- The frame class is a subclass of window. A frame object is a special window with border, title, the content display area, and an optional menu bar.

3.   Dialog- A Dialog is a top-level window typically used as a popup window and to take some form of input from the user. It comes with a border, a title bar, and a content display area.

4.   Panel- The Panel is the container that contains buttons, text field etc. It can have components button, text field etc.

Creating AWT Container:- 

1.  Empty frame with a title 

Import java.awt.*;
public class EmptyFrame 
{ 
Public static void main(String s[]) 
{ 
Frame f=new Frame(“my frame”); 
f.setSize(200,100); 
f.setVisible(true); 
}} 
2.  Use of AWT Panel:- 

Import java.awt.*; 
Public class PenelDemo 
{ 
Public static void main(String s[]) 
{ 
Frame f=new Frame(“PanelDemo”); 
Panel p=new Panel(); 
f.setLayout(new FlowLayout()); 
p.setLayout(new GridLayout(3,2)); 
f.setSize(200,120); 
p.add(new Label("login")); 
p.add(new TextField()); 
p.add(new Label("password")); 
TextField t=new TextField(); 
p.add(t); 
t.setEchoChar('*'); 
Button b=new Button("submit"); 
p.add(new Button("cancel")); 
f.add(p); 
f.setVisible(true); 
}}

Adding AWT Components:-

   ·       Adding a Label:-

Import java.awt.*;
Public class LabelDemo
{
Public static void main(String s[])
{
Frame f=new Frame(“Label Demo”);
f.setSize(200,100);
Label l=new Label(“Hello world”);
f.add(l);
f.setVisible(true);
}}

    ·       Adding a Button:-

Import java.awt.*;
Public class ButtonDemo
{
Public static void main(String s[])
{
Frame f=new Frame(“Button Demo”);
F.setSize(200,80);
f.setLayout(new FlowLayout());
Button b=new Button(“Exit”);
f.add(b);
f.setVisible(true);
}}