Unit 1 - AWT (ABSTRACT WINDOW TOOLKIT)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

UNIT 1: AWT

Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI)
or windows-based applications in Java.

Java AWT components are platform-dependent i.e. components are displayed


according to the view of operating system. AWT is heavy weight i.e. its components are
using the resources of underlying operating system (OS). The java.awt package
provides classes for AWT API such as TextField, Label, TextArea, RadioButton,
CheckBox, Choice, List etc.

Why AWT is platform independent?

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:

There are four types of containers in Java AWT:

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.

Useful Methods of Component Class

Method Description

public void add(Component c) Inserts a component on this component.

public void setSize(int width,int Sets the size (width and height) of the

height) component.

public void Defines the layout manager for the

setLayout(LayoutManager m) component.

public void setVisible(boolean Changes the visibility of the component, by

status) default false.

Java AWT Example


To create simple AWT example, you need a frame. There are two ways to create a GUI
using Frame in AWT.

1. By extending Frame class (inheritance)

2. By creating the object of Frame class (association)

AWT Example by Inheritance


Let's see a simple example of AWT where we are inheriting Frame class. Here, we are
showing Button component on the Frame.

AWTExample1.java

1. // importing Java AWT class


2. import java.awt.*;
3.
4. // extending Frame class to our class AWTExample1
5. public class AWTExample1 extends Frame {
6.
7. // initializing using constructor
8. AWTExample1() {
9.
10. // creating a button
11. Button b = new Button("Click Me!!");
12.
13. // setting button position on screen
14. b.setBounds(30,100,80,30);
15.
16. // adding button into frame
17. add(b);
18.
19. // frame size 300 width and 300 height
20. setSize(300,300);
21.
22. // setting the title of Frame
23. setTitle("This is our basic AWT example");
24.
25. // no layout manager
26. setLayout(null);
27.
28. // now frame will be visible, by default it is not visible
29. setVisible(true);
30. }
31.
32. // main method
33. public static void main(String args[]) {
34.
35. // creating instance of Frame class
36. AWTExample1 f = new AWTExample1();
37.
38. }
39.
40. }

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.

AWT Example by Association

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

1. // importing Java AWT class


2. import java.awt.*;
3.
4. // class AWTExample2 directly creates instance of Frame class
5. class AWTExample2 {
6.
7. // initializing using constructor
8. AWTExample2() {
9.
10. // creating a Frame
11. Frame f = new Frame();
12.
13. // creating a Label
14. Label l = new Label("Employee id:");
15.
16. // creating a Button
17. Button b = new Button("Submit");
18.
19. // creating a TextField
20. TextField t = new TextField();
21.
22. // setting position of above components in the frame
23. l.setBounds(20, 80, 80, 30);
24. t.setBounds(20, 100, 80, 30);
25. b.setBounds(100, 100, 80, 30);
26.
27. // adding components into frame
28. f.add(b);
29. f.add(l);
30. f.add(t);
31.
32. // frame size 300 width and 300 height
33. f.setSize(400,300);
34.
35. // setting the title of frame
36. f.setTitle("Employee info");
37.
38. // no layout
39. f.setLayout(null);
40.
41. // setting visibility of frame
42. f.setVisible(true);
43. }
44.
45. // main method
46. public static void main(String args[]) {
47.
48. // creating instance of Frame class
49. AWTExample2 awt_obj = new AWTExample2();
50.
51. }
52. }

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)

BUTTON CLASS METHODS

Sr. Method Description


no.

1. void setText (String text) It sets the string message on the button

2. String getText() It fetches the String message on the button.

3. void setLabel (String It sets the label of button with the specified

label) string.

4. String getLabel() It fetches the label of the button.

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. }

Java AWT Label

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.

Label class Constructors

Sr. Constructor Description


no.

1. Label() It constructs an empty label.

2. Label(String text) It constructs a label with the given string (left

justified by default).

3. Label(String text, int It constructs a label with the specified string and

alignement) the specified alignment.

Label Class Methods


r. Method name Description
no.

1. void setText(String text) It sets the texts for label with the specified

text.

2. void setAlignment(int It sets the alignment for label with the

alignment) specified alignment.

3. String getText() It gets the text of the label

4. int getAlignment() It gets the current alignment of the label.

Java AWT Label Example

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. }

Java AWT TextField

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.

TextField Class constructors


Sr. Constructor Description
no.

1. TextField() It constructs a new text field component.

2. TextField(String text) It constructs a new text field initialized with the

given string text to be displayed.

3. TextField(int columns) It constructs a new textfield (empty) with given

number of columns.

4. TextField(String text, int It constructs a new text field with the given text

columns) and given number of columns (width).

1. // importing AWT class


2. import java.awt.*;
3. public class TextFieldExample1 {
4. // main method
5. public static void main(String args[]) {
6. // creating a frame
7. Frame f = new Frame("TextField Example");
8.
9. // creating objects of textfield
10. TextField t1, t2;
11. // instantiating the textfield objects
12. // setting the location of those objects in the frame
13. t1 = new TextField("Welcome to Javatpoint.");
14. t1.setBounds(50, 100, 200, 30);
15. t2 = new TextField("AWT Tutorial");
16. t2.setBounds(50, 150, 200, 30);
17. // adding the components to frame
18. f.add(t1);
19. f.add(t2);
20. // setting size, layout and visibility of frame
21. f.setSize(400,400);
22. f.setLayout(null);
23. f.setVisible(true);
24. }
25. } OUTPUT

Java AWT TextArea

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.

Java AWT TextArea Example


The below example illustrates the simple implementation of TextArea where we are
creating a text area using the constructor TextArea(String text) and adding it to the
frame.

TextAreaExample .java

1. //importing AWT class


2. import java.awt.*;
3. public class TextAreaExample
4. {
5. // constructor to initialize
6. TextAreaExample() {
7. // creating a frame
8. Frame f = new Frame();
9. // creating a text area
10. TextArea area = new TextArea("Welcome to javatpoint");
11. // setting location of text area in frame
12. area.setBounds(10, 30, 300, 300);
13. // adding text area to frame
14. f.add(area);
15. // setting size, layout and visibility of frame
16. f.setSize(400, 400);
17. f.setLayout(null);
18. f.setVisible(true);
19. }
20. // main method
21. public static void main(String args[])
22. {
23. new TextAreaExample();
24. }
25. }

Output:

Java AWT Checkbox


The Checkbox class is used to create a checkbox. It is used to turn an option on (true)
or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to
"on".

Checkbox Class Constructors


Sr. Constructor Description
no.

1. Checkbox() It constructs a checkbox with no string as

the label.
2. Checkbox(String label) It constructs a checkbox with the given

label.

3. Checkbox(String label, boolean It constructs a checkbox with the given

state) label and sets the given state.

4. Checkbox(String label, boolean It constructs a checkbox with the given

state, CheckboxGroup group) label, set the given state in the specified

checkbox group.

5. Checkbox(String label, It constructs a checkbox with the given

CheckboxGroup group, boolean label, in the given checkbox group and set to

state) the specified state.

Java AWT Checkbox Example


In the following example we are creating two checkboxes using the Checkbox(String
label) constructo and adding them into the Frame using add() method.

1. // importing AWT class


2. import java.awt.*;
3. public class CheckboxExample1
4. {
5. // constructor to initialize
6. CheckboxExample1() {
7. // creating the frame with the title
8. Frame f = new Frame("Checkbox Example");
9. // creating the checkboxes
10. Checkbox checkbox1 = new Checkbox("C++");
11. checkbox1.setBounds(100, 100, 50, 50);
12. Checkbox checkbox2 = new Checkbox("Java", true);
13. // setting location of checkbox in frame
14. checkbox2.setBounds(100, 150, 50, 50);
15. // adding checkboxes to frame
16. f.add(checkbox1);
17. f.add(checkbox2);
18.
19. // setting size, layout and visibility of frame
20. f.setSize(400,400);
21. f.setLayout(null);
22. f.setVisible(true);
23. }
24. // main method
25. public static void main (String args[])
26. {
27. new CheckboxExample1();
28. }
29. }

Java AWT CheckboxGroup


The object of CheckboxGroup class is used to group together a set of Checkbox. At a
time only one check box button is allowed to be in "on" state and remaining check box
button in "off" state. It inherits the object class.

Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special
control for creating radio buttons in AWT.

Java AWT CheckboxGroup Example


1. import java.awt.*;
2. public class CheckboxGroupExample
3. {
4. CheckboxGroupExample(){
5. Frame f= new Frame("CheckboxGroup Example");
6. CheckboxGroup cbg = new CheckboxGroup();
7. Checkbox checkBox1 = new Checkbox("C++", cbg, false);
8. checkBox1.setBounds(100,100, 50,50);
9. Checkbox checkBox2 = new Checkbox("Java", cbg, true);
10. checkBox2.setBounds(100,150, 50,50);
11. f.add(checkBox1);
12. f.add(checkBox2);
13. f.setSize(400,400);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. public static void main(String args[])
18. {
19. new CheckboxGroupExample();
20. }
21. }
Output:

Java AWT Choice

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.

Java AWT Choice Example

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.

1. // importing awt class


2. import java.awt.*;
3. public class ChoiceExample1 {
4.
5. // class constructor
6. ChoiceExample1() {
7.
8. // creating a frame
9. Frame f = new Frame();
10.
11. // creating a choice component
12. Choice c = new Choice();
13.
14. // setting the bounds of choice menu
15. c.setBounds(100, 100, 75, 75);
16.
17. // adding items to the choice menu
18. c.add("Item 1");
19. c.add("Item 2");
20. c.add("Item 3");
21. c.add("Item 4");
22. c.add("Item 5");
23.
24. // adding choice menu to frame
25. f.add(c);
26.
27. // setting size, layout and visibility of frame
28. f.setSize(400, 400);
29. f.setLayout(null);
30. f.setVisible(true);
31. }
32.
33. // main method
34. public static void main(String args[])
35. {
36. new ChoiceExample1();
37. } 38. }
Java AWT List
The object of List class represents a list of text items. With the help of the List class,
user can choose either one item or multiple items. It inherits the Component class.

AWT List Class Constructors

Constructor Description

List() It constructs a new scrolling list.

List(int row_num) It constructs a new scrolling list

initialized with the given number of rows

visible.

List(int row_num, It constructs a new scrolling list

Boolean initialized which displays the given

multipleMode) number of rows.


Java AWT List Example
In the following example, we are creating a List component with 5 rows and adding it
into the Frame.

1. // importing awt class


2. import java.awt.*;
3.
4. public class ListExample1
5. {
6. // class constructor
7. ListExample1() {
8. // creating the frame
9. Frame f = new Frame();
10. // creating the list of 5 rows
11. List l1 = new List(5);
12.
13. // setting the position of list component
14. l1.setBounds(100, 100, 75, 75);
15.
16. // adding list items into the list
17. l1.add("Item 1");
18. l1.add("Item 2");
19. l1.add("Item 3");
20. l1.add("Item 4");
21. l1.add("Item 5");
22.
23. // adding the list to frame
24. f.add(l1);
25.
26. // setting size, layout and visibility of frame
27. f.setSize(400, 400);
28. f.setLayout(null);
29. f.setVisible(true);
30. }
31.
32. // main method
33. public static void main(String args[])
34. {
35. new ListExample1();
36. }
37. }

Compare list and choice

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

Java AWT Scrollbar


The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is
a GUI component allows us to see invisible number of rows and columns.

It can be added to top-level container like Frame or a component like Panel. The
Scrollbar class extends the Component class.

○ static int HORIZONTAL - It is a constant to indicate a horizontal scroll bar.

○ static int VERTICAL - It is a constant to indicate a vertical scroll bar.

Scrollbar Class Constructors

Sr. Constructor Description


no.
1 Scrollbar() Constructs a new vertical scroll bar.

2 Scrollbar(int orientation) Constructs a new scroll bar with the

specified orientation.

3 Scrollbar(int orientation, int Constructs a new scroll bar with the

value, int visible, int minimum, specified orientation, initial value, visible

int maximum) amount, and minimum and maximum values.

○ orientation: specifiey whether the scrollbar will be horizontal or vertical.

○ Value: specify the starting position of the knob of Scrollbar on its track.

○ Minimum: specify the minimum width of track on which scrollbar is moving.

○ Maximum: specify the maximum width of track on which scrollbar is moving.

Java AWT Scrollbar Example

In the following example, we are creating a scrollbar using the Scrollbar() and adding it
into the Frame.

ScrollbarExample1.java

1. // importing awt package


2. import java.awt.*;
3.
4. public class ScrollbarExample1 {
5.
6. // class constructor
7. ScrollbarExample1() {
8.
9. // creating a frame
10. Frame f = new Frame("Scrollbar Example");
11. // creating a scroll bar
12. Scrollbar s = new Scrollbar();
13.
14. // setting the position of scroll bar
15. s.setBounds (100, 100, 50, 100);
16.
17. // adding scroll bar to the frame
18. f.add(s);
19.
20. // setting size, layout and visibility of frame
21. f.setSize(400, 400);
22. f.setLayout(null);
23. f.setVisible(true);
24.}
25.
26.// main method
27.public static void main(String args[]) {
28. new ScrollbarExample1();
29.}
30.}
Java AWT MenuItem and Menu
The object of MenuItem class adds a simple labeled menu item on menu. The items
used in a menu must belong to the MenuItem or any of its subclass.

The object of Menu class is a pull down menu component which is displayed on the
menu bar. It inherits the MenuItem class.

Java AWT MenuItem and Menu Example


1. import java.awt.*;
2. class MenuExample
3. {
4. MenuExample(){
5. Frame f= new Frame("Menu and MenuItem Example");
6. MenuBar mb=new MenuBar();
7. Menu menu=new Menu("Menu");
8. Menu submenu=new Menu("Sub Menu");
9. MenuItem i1=new MenuItem("Item 1");
10. MenuItem i2=new MenuItem("Item 2");
11. MenuItem i3=new MenuItem("Item 3");
12. MenuItem i4=new MenuItem("Item 4");
13. MenuItem i5=new MenuItem("Item 5");
14. menu.add(i1);
15. menu.add(i2);
16. menu.add(i3);
17. submenu.add(i4);
18. submenu.add(i5);
19. menu.add(submenu);
20. mb.add(menu);
21. f.setMenuBar(mb);
22. f.setSize(400,400);
23. f.setLayout(null);
24. f.setVisible(true);
25.}
26.public static void main(String args[])
27.{
28.new MenuExample();
29.}
30.}

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:

1. public static final int NORTH

2. public static final int SOUTH

3. public static final int EAST

4. public static final int WEST

5. public static final int CENTER

Constructors of BorderLayout class:

You might also like