Unit 1 - AWT (ABSTRACT WINDOW TOOLKIT)
Unit 1 - AWT (ABSTRACT WINDOW TOOLKIT)
Unit 1 - AWT (ABSTRACT WINDOW TOOLKIT)
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI)
or windows-based applications in Java.
Java AWT calls the native platform calls the native platform (operating systems)
subroutine for creating API components like TextField, ChechBox, button, etc.
For example, an AWT GUI with components like TextField, label and button will have
different look and feel for the different platforms like Windows, MAC OS, and Unix. The
reason for this is the platforms have different view for their native components and
AWT directly calls the native subroutine that creates those components.
In simple words, an AWT application will look like a windows application in Windows OS
whereas it will look like a Mac application in the MAC OS.
Java AWT Hierarchy The hierarchy of Java AWT classes are given below.
Components
All the elements like the button, text fields, scroll bars, etc. are called components. In
Java AWT, there are classes for each component as shown in above diagram. In order to
place every component in a particular position on a screen, we need to add them to a
container.
Container
The Container is a component in AWT that can contain another components like
buttons, textfields, labels etc. The classes that extends Container class are known as
container such as Frame, Dialog and Panel.
It is basically a screen where the where the components are placed at their specific
locations. Thus it contains and controls the layout of components.
Note: A container itself is a component (see the above diagram), therefore we can add a
container inside container.
Types of containers:
1. Window
2. Panel
3. Frame
4. Dialog
Window
The window is the container that have no borders and menu bars. You must use frame,
dialog or another window for creating a window. We need to create an instance of
Window class to create this container.
Panel
The Panel is the container that doesn't contain title bar, border or menu bar. It is generic
container for holding the components. It can have other components like button, text
field etc. An instance of Panel class creates a container, in which we can add
components.
Frame
The Frame is the container that contain title bar and border and can have menu bars. It
can have other components like button, text field, scrollbar etc. Frame is most widely
used container while developing an AWT application.
Method Description
public void setSize(int width,int Sets the size (width and height) of the
height) component.
setLayout(LayoutManager m) component.
AWTExample1.java
The setBounds(int x-axis, int y-axis, int width, int height) method is used in the above
example that sets the position of the awt button.
Let's see a simple example of AWT where we are creating instance of Frame class.
Here, we are creating a TextField, Label and Button component on the Frame.
AWTExample2.java
JAVA AWT BUTTON: A button is basically a control component with a label that
generates an event when pushed. The Button class is used to create a labeled button
that has platform independent implementation. The application result in some action
when the button is pushed.
1 Button( ) It constructs a new button with an empty string i.e. it has no
. label.
2 Button (String It constructs a new button with given string as its label.
. text)
1. void setText (String text) It sets the string message on the button
3. void setLabel (String It sets the label of button with the specified
label) string.
ButtonExample.java
1. import java.awt.*;
2. public class ButtonExample {
3. public static void main (String[] args) {
4.
5. // create instance of frame with the label
6. Frame f = new Frame("Button Example");
7.
8. // create instance of button with label
9. Button b = new Button("Click Here");
10.
11. // set the position for the button in frame
12. b.setBounds(50,100,80,30);
13.
14. // add button to the frame
15. f.add(b);
16. // set size, layout and visibility of frame
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. }
The object of the Label class is a component for placing text in a container. It is used to
display a single line of read only text. The text can be changed by a programmer but a
user cannot edit it directly.
It is called a passive control as it does not create any event when it is accessed. To
create a label, we need to create the object of Label class.
AWT Label Fields
The java.awt.Component class has following fields:
1. static int LEFT: It specifies that the label should be left justified.
2. static int RIGHT: It specifies that the label should be right justified.
3. static int CENTER: It specifies that the label should be placed in center.
justified by default).
3. Label(String text, int It constructs a label with the specified string and
1. void setText(String text) It sets the texts for label with the specified
text.
In the following example, we are creating two labels l1 and l2 using the Label(String
text) constructor and adding them into the frame.
LabelExample.java
1. import java.awt.*;
2.
3. public class LabelExample {
4. public static void main(String args[]){
5.
6. // creating the object of Frame class and Label class
7. Frame f = new Frame ("Label example");
8. Label l1, l2;
9.
10. // initializing the labels
11. l1 = new Label ("First Label.");
12. l2 = new Label ("Second Label.");
13.
14. // set the location of label
15. l1.setBounds(50, 100, 100, 30);
16. l2.setBounds(50, 150, 100, 30);
17.
18. // adding labels to the frame
19. f.add(l1);
20. f.add(l2);
21.
22. // setting size, layout and visibility of frame
23. f.setSize(400,400);
24. f.setLayout(null);
25. f.setVisible(true);
26. }
27. }
The object of a TextField class is a text component that allows a user to enter a single
line text and edit it. It inherits TextComponent class, which further inherits Component
class.
When we enter a key in the text field (like key pressed, key released or key typed), the
event is sent to TextField. Then the KeyEvent is passed to the registered KeyListener. It
can also be done using ActionEvent; if the ActionEvent is enabled on the text field, then
the ActionEvent may be fired by pressing return key. The event is handled by the
ActionListener interface.
number of columns.
4. TextField(String text, int It constructs a new text field with the given text
The object of a TextArea class is a multiline region that displays text. It allows the
editing of multiple line text. It inherits TextComponent class.
The text area allows us to type as much text as we want. When the text in the text area
becomes larger than the viewable area, the scroll bar appears automatically which helps
us to scroll the text up and down, or right and left.
TextAreaExample .java
Output:
the label.
2. Checkbox(String label) It constructs a checkbox with the given
label.
state, CheckboxGroup group) label, set the given state in the specified
checkbox group.
CheckboxGroup group, boolean label, in the given checkbox group and set to
Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special
control for creating radio buttons in AWT.
The object of Choice class is used to show popup menu of choices. Choice selected by
user is shown on the top of a menu. It inherits Component class.
In the following example, we are creating a choice menu using Choice() constructor.
Then we add 5 items to the menu using add() method and Then add the choice menu
into the Frame.
Constructor Description
visible.
Choice : A Choice is displayed in a compact form that requires you to pull it down
to see the list of available choices. Only one item may be selected from a Choice.
List : A List may be displayed in such a way that several List items are visible. A
List supports the selection of one or more List items.
The java.awt.Choice component implements a list of items where only the
selected item is displayed. The list appears as a drop down (pop-down menu)
menu can be seen through the touch of a button built into the component, so it is
also known as a check box or combobox. In the same manner as in component
java.awt.List a vertical scrollbar is automatically displayed when the list can not
simultaneously show all the items they contain.
Scrollbar
It can be added to top-level container like Frame or a component like Panel. The
Scrollbar class extends the Component class.
specified orientation.
value, int visible, int minimum, specified orientation, initial value, visible
○ Value: specify the starting position of the knob of Scrollbar on its track.
In the following example, we are creating a scrollbar using the Scrollbar() and adding it
into the Frame.
ScrollbarExample1.java
The object of Menu class is a pull down menu component which is displayed on the
menu bar. It inherits the MenuItem class.
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east,
west, and center. Each region (area) may contain one component only. It is the default
layout of a frame or window. The BorderLayout provides five constants for each region: