Professional Documents
Culture Documents
Abstract Windows Toolkit
Abstract Windows Toolkit
Abstract Windows Toolkit
AWT package is bundled with Java software by default, i.e., located in rt.jar file (jre/lib).
AWT package is platform dependent API used for creating a graphical user interface.
The AWT contains a number of classes and methods that allow you to create and
manage windows. It is also the foundation to build Swing. When a user wants to interact
with an application, the user has to provide some information to the application and it
can be done in two ways:
Graphical User Interface(GUI): This interface will interact with the application by
using some graphics like menus, icons, images, etc. This interface is user friendly
because it prompts the user by providing the options or menus. Example:
Windows XP, Windows 7, etc.
Points To Remember:
Note: AWT is rarely used nowadays because of its platform-dependent and heavy-
weight nature. AWT components are considered heavyweight because they are being
generated by the underlying operating system(OS).
1. They are easy to learn and use, i.e. users without experience can also use the
system quickly.
2. The user can interact with several different applications by switching quickly from
one task to another.
3. Information remains visible in its own window when attention is switched.
4. Fast, full-screen interaction is possible with immediate access to anywhere on the
screen.
5. Some types of error situations can be avoided because they are not allowed to
occur through input.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI)
programming. AWT features include:
AWT UI Aspects
1. UI Elements: It refers to the core visual elements used for interacting with the
applications and which are visible to the users.
3. Behavior: It defines the events which should occur when a user interacts with UI
elements.
AWT Hierarchy in Java
The AWT classes are contained in 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 than you might at first believe.
The AWT defines windows according to a class hierarchy that is used to add
functionality and specificity to each level. The two most common windows are Panel,
which is used by applets, and Frame, which creates a standard application window.
Most of the functionalities of these windows are derived from their parent classes.
At the top of the AWT, the hierarchy is the component class. The Component is an
abstract class that encapsulates all of the attributes of a visual component. All user
interface elements that interact with the user are subclasses of Component and are
displayed on the screen.
Container
The Container class is a subclass of the Component class. It has additional methods
that allow other component objects to be nested within it. A container provides a space
where a component can be located. A Container in AWT is a component itself and it
adds the capability to add the component to itself.
Panel
The Panel class is a concrete subclass of Container. It provides space in which an
application can attach any other component. The default layout manager for a panel is
the FlowLayout layout manager. It is visually represented as a window that does not
contain a title bar, menu bar, or border. Panels don’t have any visible bounding lines.
You can delimit them with different background colors.
Window
The Window class creates a top-level window with no border and no menubar. It uses
BorderLayout as a default layout manager. A window must have either a frame, dialog,
or another window defined as its owner when it’s constructed. It could be used to
implement a pop-up menu. A Window object blocks input to other application windows
when it is shown.
Dialog
Dialog class’s instance always needs an associated Frame class instance to exist.
Dialog class is also a subclass of Window and comes with the border as well as the title.
An instance of the Dialog class cannot exist without an associated instance of the Frame
class.
Frame
A-Frame may be a top-level window with a title and a border. It can produce other
components like buttons, text fields, etc. The default layout for a frame is BorderLayout.
Frames are capable of generating the subsequent sorts of window events:
WindowOpened, WindowClosing, WindowClosed, WindowIconified, WindowDeiconified,
WindowActivated, WindowDeactivated. When a Frame window is made by a stand-
alone application instead of an applet, a traditional window is made.
Frame’s Constructors
Methods of Frames
3. Dimension getSize() : The getSize() method is used to obtain the current size of
a window. This method returns the current size of the window contained within
the width and height fields of a Dimension object.
5. void setTitle(String newTitle): You can change the title in a frame window using
setTitle(). Here, newTitle is the new title for the window.
6. windowClosing(): 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.
2. Create the object of any class that extends the Frame class.
import java.awt.*;
f.add (lb);
f.setVisible (true);
}
}
Output:
Example to create a frame by extending the frame class using AWT in Java
import java.awt.*;
fd.add (btn);
fd.setVisible (true);
}
}
Output:
AWT Controls in Java:
Controls are components that allow a user to interact with your application in various
ways. The AWT supports the following types of controls:
Labels
The easiest control to use is a label. A label contains a string and is an object of type
Label. Labels are passive controls that do not support any interaction with the user.
Label Constructors:
Label Methods:
1. void setText(String str): It is used to set or change the text in a label by using
the setText() method. Here, str specifies the new label.
2. String getText(): It is used to obtain the current label by calling getText()
method. Here, the current label is returned.
3. void setAlignment(int how): It is used to set the alignment of the string within
the label by calling setAlignment() method. Here, how is one of the alignment
constants?
4. int getAlignment(): It is used to obtain the current alignment, getAlignment() is
called.
import java.awt.*;
import java.applet.*;
/*
<applet code = "LabelDemo" width=300 height=200>
</applet>
*/
public class LabelDemo extends Applet
{
public void init ()
{
Label one = new Label ("One");
Label two = new Label ("Two");
Label three = new Label ("Three");
add (one);
add (two);
add (three);
}
}
Output:
The most widely used control is Button. A button is a component that contains a label
and that generates an event when it is pressed.
Button Constructors:
Button Methods :
1. void setLabel(String str): You can set its label by calling setLabel(). Here, str is
the new Label for the button.
2. String getLabel(): You can retrieve its label by calling getLabel() method.
Example to understand AWT Button Control in Java:
import java.awt.*;
import java.awt.event.*;
ButtonDemo ()
this.setLayout (null);
this.add (b1);
this.add (b2);
this.setVisible (true);
this.setTitle ("button");
System.exit (0);
}
});
Output:
Canvas Constructor:
Canvas Methods:
import java.awt.*;
import java.awt.event.*;
public class CanvasDemo
public CanvasDemo ()
prepareGUI ();
awtControlDemo.showCanvasDemo ();
{
System.exit (0);
});
headerLabel.setAlignment (Label.CENTER);
statusLabel.setAlignment (Label.CENTER);
mainFrame.add (headerLabel);
mainFrame.add (controlPanel);
mainFrame.add (statusLabel);
mainFrame.setVisible (true);
mainFrame.setVisible (true);
public MyCanvas ()
{
setBackground (Color.GRAY);
Graphics2D g2;
g2 = (Graphics2D) g;
Output:
AWT Checkbox Control in java
A checkbox may be a control that’s used to turn an option on or off. It consists of a little
box that will either contain a check or not. There’s a label related to each checkbox that
describes what option the box represents. You modify the state of a checkbox by
clicking on. Checkboxes are often used individually or as a part of a gaggle. Checkboxes
are objects of the Checkbox class.
Checkbox Constructor
Methods of Checkbox
2. void setState(boolean on): To line its state, call setState(). Here, if one is true,
the box is checked. If it’s false, the box is cleared.
3. String getLabel(): you’ll obtain the present label related to a checkbox by calling
getLabel().
4. void setLabel(String str): To line the label, call setLabel(). The string passed in
str becomes the new label related to the invoking checkbox.
Example to understand AWT Checkbox Control in Java:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add (winXP);
add (winVista);
add (solaris);
add (mac);
winXP.addItemListener (this);
winVista.addItemListener (this);
solaris.addItemListener (this);
mac.addItemListener (this);
}
public void itemStateChanged (ItemEvent ie)
repaint ();
Output:
CheckboxGroup: Radio Buttons
It is possible to make a group of mutually exclusive checkboxes during which one and just
one checkbox up the group are often checked at anybody time. These checkboxes are
often called radio buttons because they act just like the station selector on a car radio,
only one station is often selected at anybody’s time. To create a group of mutually
exclusive checkboxes, you want to first define the group to which they’re going to belong
then specify that group once you construct the checkboxes. Checkbox groups are objects
of the type CheckboxGroup. Only the default constructor is defined, which creates an
empty group.
Creating Radiobutton :
CheckboxGroup Methods
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
CheckboxGroup cbg;
add (winXP);
add (winVista);
add (solaris);
add (mac);
winXP.addItemListener (this);
winVista.addItemListener (this);
solaris.addItemListener (this);
mac.addItemListener (this);
}
repaint ();
Output:
This component will display a group of times as a drop-down menu from which a user
can select only one item. The choice component is used to create a pop-up list of items
from which the user may choose. Therefore, Choice control is a form of a menu. When it
is inactive, a Choice component takes up only enough space to show the currently
selected item. When the user clicks on a Choice component, the whole list of choices
pops up, and a new selection can be made.
Note: Choice only defines the default constructor, which creates an empty list.
Creating Choice : Choice ch = new Choice();
Choice Methods
1. void add(String name): To add a selection to the list, use add(). Here, the name
is the name of the item being added.
2. String getSelectedItem(): It determines which item is currently selected. It
returns a string containing the name of the item.
3. int getSelectedIndex(): It determines which item is currently selected. It returns
the index of the item.
4. int getItemCount(): It obtains the number of items in the list.
5. void select(int index): It is used to set the currently selected item with a zero-
based integer index.
6. void select(String name): It is used to set the currently selected item with a
string that will match a name in the list.
7. String getItem(int index): It is used to obtain the name associated with the item
at the given index. Here, the index specifies the index of the desired items.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="ChoiceDemo" width=300 height=180> </applet> */
This component will display a group of items as a drop-down menu from which a user
can select only one item. The List class provides a compact, multiple-choice, scrolling
selection list. Unlike the selection object, which shows only the only selected item within
the menu, an inventory object is often constructed to point out any number of choices
within the visible window. It also can be created to permit multiple selections.
List Constructor
1. List() throws HeadlessException: It creates a list control that allows only one
item to be selected at any one time.
2. List(int numRows) throws HeadlessException: Here, the value of numRows
specifies the number of entries in the list that will always be visible.
3. List(int numRows, boolean multipleSelect) throws HeadlessException: If
multipleSelect is true, then the user may select two or more items at a time. If it’s
false, then just one item could also be selected.
Method of Lists
1. void add(String name): To add a selection to the list, use add(). Here, the name
is the name of the item being added. It adds items to the end of the list.
2. void add(String name, int index): It also adds items to the list but it adds the
items at the index specified by the index.
3. String getSelectedItem(): It determines which item is currently selected. It
returns a string containing the name of the item. If more than one item is
selected, or if no selection has been made yet, null is returned.
4. int getSelectedIndex(): It determines which item is currently selected. It returns
the index of the item. The first item is at index 0. If more than one item is
selected, or if no selection has yet been made, -1 is returned.
5. String[] getSelectedItems(): It allows multiple selections. It returns an array
containing the names of the currently selected items.
6. int[] getSelectedIndexes(): It also allows multiple selections. It returns an array
containing the indexes of the currently selected items.
7. int getItemCount(): It obtains the number of items in the list.
8. void select(int index): It is used to set the currently selected item with a zero-
based integer index.
9. String getItem(int index): It is used to obtain the name associated with the item
at the given index. Here, the index specifies the index of the desired items.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
os.add ("Solaris");
browser.add ("Firefox");
browser.add ("Opera");
browser.select (1);
add (os);
add (browser);
os.addActionListener (this);
browser.addActionListener (this);
repaint ();
int idx[];
Output:
Scrollbars are used to select continuous values between a specified minimum and
maximum. Scroll bars may be oriented horizontally or vertically. A scroll bar is really a
composite of several individual parts. Each end has an arrow that you simply can click to
get the present value of the scroll bar one unit within the direction of the arrow. The
current value of the scroll bar relative to its minimum and maximum values are indicated
by the slider box for the scroll bar. Scrollbars are encapsulated by the Scrollbar class.
Scrollbar Constructor
Scrollbar Methods
1. void setValues(int initialValue, int thumbSize, int min, int max): It is used to
set the parameters of the constructors.
2. int getValue(): It is used to obtain the current value of the scroll bar. It returns the
current setting.
3. void setValue(int newValue): It is used to set the current value. Here, newValue
specifies the new value for the scroll bar. When you set a worth, the slider box
inside the scroll bar is going to be positioned to reflect the new value.
4. int getMaximum(): It is used to retrieve the minimum values. They return the
requested quantity. By default, 1 is the increment added to the scroll bar.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add (vertSB);
add (horzSB);
addMouseMotionListener (this);
repaint ();
vertSB.setValue (y);
horzSB.setValue (x);
repaint ();
}}
Output
1. TextField
2. TextArea
The TextField component will allow the user to enter some text. It is used to implement a
single-line text-entry area, usually called an edit control. It also allows the user to enter
strings and edit the text using the arrow keys, cut and paste keys, and mouse selections.
TextField is a subclass of TextComponent.
TextField Constructors
TextField Methods
1. String getText(): It is used to obtain the string currently contained in the text
field.
2. void setText(String str): It is used to set the text. Here, str is the new String.
3. void select(int startIndex, int endIndex): It is used to select a portion of the text
under program control. It selects the characters beginning at startIndex and
ending at endIndex-1.
8. boolean echoCharIsSet(): By this method, you can check a text field to see if it
is in this mode.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
add (namep);
add (name);
add (passp);
add (pass);
name.addActionListener (this);
pass.addActionListener (this);
repaint ();
Output
AWT TextArea Control in Java
Sometimes one line of text input isn’t enough for a given task. To handle these
situations, the AWT includes an easy multiline editor called TextArea.
TextArea Constructor
1. SCROLLBARS_BOTH
2. SCROLLBARS_NONE
3. SCROLLBARS_HORIZONTAL_ONLY
4. SCROLLBARS_VERTICAL_ONLY
TextArea Methods
1. void append(String str): It appends the string specified by str to the end of the
current text.
2. void insert(String str, int index): It inserts the string passed in str at the
specified index.
3. voidReplaceRange(String str, int startIndex, int endIndex): It is used to
replace the text. It replaces the characters from startIndex to endIndex-1.
import java.awt.*;
import java.applet.*; /* <applet code="TextAreaDemo" width=300 height=250> </applet>
*/
Output