Adj 01
Adj 01
Introduction to AWT :
● The Java AWT utilizes the native platform subroutine to create API
components such as TextField, CheckBox, and buttons.
● This results in a different visual format for these components on
different platforms such as Windows, MAC OS, and Unix.
● The reason for this is that each platform has a distinct view of its
native components.
● AWT directly calls this native subroutine to create the components,
resulting in an AWT application resembling a Windows application
on Windows OS, and a Mac application on the MAC OS. In simpler
terms, the AWT application’s appearance adapts to the platform it is
running on.
● AWT is platform independent even after the AWT components are
platform dependent because of the points mentioned below:
1. JVM (Java Virtual Machine):As Java Virtual Machine is platform
dependent
2. Abstract APIs:AWT provides an abstract layer for GUI. Java
applications interact with AWT through Abstract API which are
platform independent. Abstract API allows Java to isolate
platform-specific details, making code portable across different
systems.
3. Platform-Independent Libraries: The Libraries of AWT are written
in Java which they are totally platform-independent. Because of this,
it ensures that AWT functionality remains consistent across different
environments.
Java AWT Hierarchy :
text fields, checkboxes, etc used for creating GUI elements for Java
Applications.
● Layout Managers: Layout Managers are responsible for arranging data in the
● Graphics and Drawing: It is the feature of AWT that helps to draw shapes,
Components are added to containers, and layout managers dictate their placement within the
container. This hierarchy allows you to build complex user interfaces by organizing smaller
components into well-structured containers.
Container
as buttons, labels, and text fields. The window is the container that
3. Frame: The Frame is the container that contains the 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
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.
Frame in Java
Hierarchy :
Component → Container → Window → Frame
Syntax :
public class Frame
extends Window
implements MenuContainer
{
}
import java.awt.*;
class Sample{
Sample(){
Frame f = new Frame();
f.setVisible(true);
f.setLayout(null);
f.setSize(500,500);
f.setTitle("hello I am a frame created by object");
}
public static void main(String args[]){
Sample s = new Sample();
}}
import java.awt.*;
class Sample extends Frame{
Sample(){
this.setVisible(true);
this.setLayout(null);
this.setSize(500,500);
this.setTitle("hello");
}
public static void main(String args[]){
Sample s = new Sample();
}
}
Panel
https://www.geeksforgeeks.org/java-awt-panel/
1. Label():
Creates Empty Label.
2. Label(String str):
Constructs a Label with str as its name.
Sr.
No. Method Description
3. void setAlignment(int
alignment) Possible values: Label.LEFT, Label.RIGHT,
Label.CENTER.
7. void addNotify() Creates the peer (native window) for the label.
Methods inherited
The Methods provided with AWT Label is inherited by classes mentioned
below:
● java.awt.Component
● java.lang.Object
Program:
import java.awt.*;
class Sample extends Frame{
Sample(){
this.setVisible(true);
this.setSize(500,500);
this.setLayout(null);
this.setTitle("Label");
}
public static void main(String args[]){
Sample s = new Sample();
}
Button in java
Syntax of Java AWT Button
Inherited Methods
● java.awt.Component
● java.lang.Object
● Note: The Button class inherits methods from java.awt.Component and
java.lang.Object classes.
https://www.geeksforgeeks.org/java-awt-button/
https://www.javatpoint.com/java-awt-button
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".
Syntax :
public class Checkbox extends Component implements ItemSelectable, Accessible
● java.awt.Component
● java.lang.Object
https://www.geeksforgeeks.org/java-awt-checkbox/?ref=ml_lbp
CheckBox Group
Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special
control for creating radio buttons in AWT.
import java.awt.*;
import java.awt.event.*;
class Sample{
Sample(){
f.setVisible(true);
f.setSize(500,500);
f.setTitle("I am a Frame");
f.setLayout(null);
c1.setBounds(50,50,100,20);
c2.setBounds(50,90,100,20);
c3.setBounds(50,130,100,20);
f.add(c1);
f.add(c2);
f.add(c3);
Label l = new Label();
l.setBounds(50,250,100,50);
f.add(l);
b1.setBounds(50,170,200,50);
f.add(b1);
b1.addActionListener(new ActionListener(){
Checkbox cg = cbg.getSelectedCheckbox();
l.setText(cg.getLabel());
}});
}}
Java AWT Scrollbar
The ‘Scrollbar’ class is a part of the ‘java.awt’ package. Java AWT Scrollbars
are used for scrolling through content like text, images, or any other data that
doesn’t fit completely within the specified display area. The Scrollbar
component supports both vertical and horizontal scrolling. It generates
events when it is controlled by the user. We can add custom behavior using
different methods to improve the functionality of our application.
Syntax of Java AWT Scrollbar
orientation.
3 Scrollbar(int orientation, int Constructs a new scroll bar with the specified
value, int visible, int minimum, orientation, initial value, visible amount, and
be horizontal or vertical.
moves.
moves.
Inherited Fields
● java.awt.Component
● java.awt.Adjustable
● java.awt.image.ImageObserver
bar.
Scroll bar.
objects.
processEvent(AWTEvent e)
16. void setMaximum (int It sets the maximum value of the scroll bar.
newMaximum)
17. void setMinimum (int It sets the minimum value of the scroll bar.
newMinimum)
18. void setOrientation (int It sets the orientation for the scroll bar.
orientation)
19. void setUnitIncrement(int v) It sets the unit increment for the scroll bar.
20. void setValue (int newValue) It sets the value of scroll bar with the given
argument value.
22. void setValues (int value, int It sets the values of four properties for
visible, int minimum, int scroll bar: value, visible amount, minimum
23. void setVisibleAmount (int It sets the visible amount of the scroll bar.
newAmount)
24. AccessibleContext It gets the accessible context related to the
currently.
Inherited Methods
● java.awt.Component
● java.lang.Object
Program:
import java.awt.*;
class Sample{
Sample(){
Frame f = new Frame("This is a frame");
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]){
Sample s = new Sample();
}}
Whenever a key is pressed or typed into the input field, an event is sent to
the TextField. The registered KeyListener then receives the KeyEvent.
ActionEvent can also be used for this purpose. If the ActionEvent is
configured on the text field, then it will be triggered when the Return key is
pressed. The ActionListener interface handles the event.
the given text to be displayed and a width that fits the specified
number of columns.
text field.
for echoing.
columns) columns.
protected String It returns a string representing state
ActionListener object.
processEvent(AWTEvent e)
field.
text.
Inherited Methods
The Methods included with AWT TextField are inherited by:
● java.awt.TextComponent
● java.lang.Component
● java.lang.Object
Programs:
import java.awt.*;
import java.awt.event.*;
class Sample{
Sample(){
Frame f = new Frame();
f.setVisible(true);
f.setTitle("I am a Frame");
f.setLayout(null);
f.setSize(500,500);
f.add(t1);
f.add(t2);
f.add(t3);
}
public static void main(String args[]){
Sample s = new Sample();
}}
TextArea
The TextArea control in AWT provide us multiline editor area. The user can type here
as much as he wants. When the text in the text area become larger than the viewable
area the scroll bar is automatically appears which help us to scroll the text up & down
and right & left. It allows the editing of multiple line text. It inherits TextComponent
class.
AWT TextArea Class Declaration
public class TextArea extends TextComponent
vertical scrollbars.
horizontal scrollbar.
vertical scrollbar.
text
TextArea Constructors
1 TextArea()
Constructs a new text area with the empty string as text.
3 TextArea(String text)
Constructs a new text area with the specified text.
4 TextArea(String text, int rows, int columns)
Constructs a new text area with the specified text, and with the specified number
of rows and columns.
Methods Inherited :
● java.awt.TextComponent
● java.awt.Component
● java.lang.Object
Program :
import java.awt.*;
import java.awt.event.*;
class Sample{
Sample(){
Frame f = new Frame("This is a Frame");
f.setVisible(true);
f.setSize(500,500);
f.setLayout(null);
Layout Manager
The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in
GUI forms. LayoutManager is an interface that is implemented by all the classes of
layout managers. There are the following classes that represent the layout managers:
1) FlowLayout
● FlowLayout is used to arrange components in a sequence one after the
other.
● The default layout of applet and panel is FlowLayout.
Programs:
import java.awt.*;
import java.awt.event.*;
class Sample{
Sample(){
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
}
public static void main(String args[]){
Sample s = new Sample();
}}
Border Layout
BorderLayout is the default layout for the window objects such as JFrame,
JWindow, JDialog, JInternalFrame etc. BorderLayout arranges the
components in the five regions. Four sides are referred to as north, south,
east, and west. The middle part is called the center. Each region can contain
only one component and is identified by a corresponding constant as NORTH,
SOUTH, EAST, WEST, and CENTER.
BorderLayout Fields
border layout.
components.
components.
GridLayout
● GridLayout class represents a layout manager with a specified number of
rows and columns in a rectangular grid.
● The GridLayout container is divided into an equal-sized of rectangles, and
one of the components is placed in each rectangle.
● Every rectangle cell has the same size therefore, they contain a
component, which fills the entire cell.
● When the user changes or adjusts the size of the container, the size of
each rectangles changes accordingly.
Constructors of the class:
1. GridLayout(): It Creates a grid layout with a default of one column per
2. GridLayout(int rw, int cl): It creates a grid layout with the specified
3. GridLayout(int rw, int cl, int hgap, int vgap): It creates a grid layout
with the specified number of rows and columns with horizontal and
vertical gap.
specified value.
specified value.
specified value.
this layout.
values.
Card Layout