Java Tutorial

AWT in Java

In JAVA we can develop to types of GUI (Graphic User Interface) applications. They are standalone GUI applications and distributed GUI applications.

  1. A standalone GUI application is one which runs in the context of local disk and our class must extends a predefined class called java.lang.Frame class.
  2. A distributed GUI application is one which runs in the context of browser and our class must extend java.applet.Applet class.

As a part of GUI applications we use to create two types of components. They are passive components and active components.

  1. A passive component is one where there is no interaction from the user. For example label.
  2. An active component is one where there is an interaction from the user. For example button, check box, scroll bar, etc.

The active and passive components in JAVA are available in terms of classes. In order to deal with any GUI applications we must import a package called java.awt.* (contains various classes and interfaces for creating GUI components) and java.awt.event.* (contains various classes and interfaces which will provide functionality to GUI components).

AWT Hierarchy chart

Whenever we develop any GUI application we must have readily available window component and window component must contain frame component. Any GUI component which we want to create gives the corresponding class and adds all those components to the object of Container class.

A Container is a class whose object allows us to add 'n' number of similar or different GUI components to make a final application.

Object class and Applet class all the classes in the above hierarchy chart are belongs to java.awt.* package.

Write a JAVA program which creates Window and Frame?

Answer:

import java.awt.*; 
class myf extends Frame
{
    Myf ()
    {
        setText (?AshaKrishna?); ---1
        setSize (100, 100);            ---2
        setBackground (Color, cyan); ---3 
        setForeground (Color, red); ---4 
        setVisible (true);       ---5
    }
};
class FDemo
{
    Public static void main (String [] args)
    {
        Myf mo=new myf ();
    }
};

The methods in line numbers 1 and 2 defined in java.awt.Window class. The methods in line numbers 3, 4 and 5 are defined in java.awt.Component class.

Level Class:

Label is a class which is used for creating label as a part of windows application. The component label comes under passive component. Labels always improve the functionality and readability of active components. Creating a label is nothing but creating an object of label components.

Label class API:

Data members:

public static final int LEFT (0) //by default the alignment is always left only
public static final int CENTER (1) 
public static final int RIGHT (2)

The above three statements are called alignment parameters or modifiers.

Constructors:

Label ()  
Label (String)
Label (String label name, int alignment modifier)

Instance methods:

public void setText (String); 
public String getText (); 
public void setAlignment (int); 
public int getAlignment ();

For example we can write a program as follows:

Label l=new Label (); 
l=setText ("ENTER EMPNO");
l=setAlignment (Label.CENTER)

or

Label l1=new Label ("ENTER EMPNAME"); 
String Label=l1.getText (); 
l1.setAlignment (Label.RIGHT);

or

Label l=new Label ("ENTER EMPSAL", Label.CENTER);
                        

Event Delegation Model:

Whenever we want to develop any windows applications one must deal with event delegation model. Event delegation model contains four properties. They are:

In order to process any active components, we must know either name or caption or label or reference of the component (object).

Whenever we interact any active component, the corresponding active component will have one predefined Event class, whose object will be created and that object contains two details:

  1. Name of the component.
  2. Reference of the component.

The general form of every Event class is xxx event.

For example:

Component name Event name
printjava.awt.event.ActionEvent
choicejava.awt.event.ItemEvent
textField java.awt.event.TextEvent
textAreajava.awt.event.TextEvent
scrollbarjava.awt.event.AdjustmentEvent

In order to perform any action or operation when an interactive component is interacted we must write some set of statements into the appropriate methods. These methods are not defined or developed by SUN micro system, but they have supplied those methods with no definition i.e., undefined methods.

In JAVA purely undefined methods are abstract methods present in interfaces. Interfaces in awt are called listeners. Hence, every interactive component must have a predefined listener whose general notation is xxx listener.

For example:

Component nameListener name
Buttonjava.awt.event.ActionListener
choicejava.awt.event.ItemListener
textField java.awt.event.TextListener
textAreajava.awt.event.TextListener
scrollbarjava.awt.event.AdjustmentListener

Identity what is the undefined method or abstract method in xxx Listener.

For example:

Component nameUndefined method name
Buttonpublic void actionPerformed (java.awt.event.ActionEvent)
choicepublic void actionPerformed (java.awt.event.ItemEvent)
textField public void actionPerformed (java.awt.event.TextEvent)
textAreapublic void actionPerformed (java.awt.event.TextEvent)

Each and every interactive component must be registered and unregistered with particular event and Listener. The general form of registration and un-registration methods is as follows:

public void addxxxListener (xxxListener);
public void removexxxListener (xxxListener);

For example:

Component nameRegistration methodUn-registration method
Buttonpublic void addActionListener     (ActionListener);public void removeActionListener (ActionListener);
choicepublic void addItemListener (ItemListener);public void removeItemListener (ItemListener);
textField public void addTextListener (TextListener);public void removeTextListener (TextListener);
textAreapublic void addTextListener (TextListener);public void removeTextListener (TextListener);