ABSTRACT WINDOW TOOLKIT (AWT)
ABSTRACT WINDOW TOOLKIT (AWT)
AWT stands for Abstract Window Toolkit.
It is a platform-dependent API used to develop GUI
(Graphical User Interface) or window-based applications
in Java.
It was developed by Sun Microsystems in 1995.
It is heavyweight because its components are generated
by the system’s host operating system.
It contains a large number of classes and methods used
for creating and managing GUI components.
AWT JAVA / UNIT - IV
JAVA SWING
JAVA SWING
Swing is a lightweight Java graphical user interface (GUI).
It is used to create various desktop applications.
Swing provides platform-independent components.
It allows users to create buttons, scroll bars, and other
GUI elements.
Swing components are written in Java language.
It is a part of the Java Foundation Classes (JFC).
AWT JAVA / UNIT - IV
Platform Independence:
FEATURES OF JAVA SWING
FEATURES OF JAVA SWING
Swing applications can run on any platform that supports
Java.
Rich Set of Components:
Provides components like buttons, text fields, labels, menus,
lists, and tables to build complex GUIs.
Customization:
Components can be easily customized based on design and
functional needs.
Event-Driven Programming:
Responds to user actions such as button clicks and mouse
events.
MVC Architecture:
Follows the Model-View-Controller pattern to separate data,
interface, and logic.
FEATURES OF JAVA SWING JAVA / UNIT - IV
DIFFERENCE BETWEEN JAVA AWT AND SWING
DIFFERENCE BETWEEN JAVA AWT AND SWING
JJ A
A VV AA AA W
W TT JJ A
A VV AA SS W
W II NN GG
Java AWT is an API to Swing is a part of Java Foundation
develop GUI applications in Classes and is used to create
Java. various applications.
The components of Java AWT The components of Java
are platform dependent. Swing are platform
independent.
The components of Java AWT The components of Java
are heavy weighted. Swing are light weighted.
AWT components require Swing components requires
[Link] package [Link] package
AWT provides comparatively Swing provides more
less powerful components. powerful components.
The execution time of AWT is The execution time of Swing
more than Swing. is less than AWT.
AWT VS SWING JAVA / UNIT - IV
HIERARCHY OF SWING COMPONENTS
HIERARCHY OF SWING COMPONENTS
In Java Swing, components are organized in a hierarchical
structure.
Swing components are nested inside one another to build
user interfaces.
This hierarchy helps in creating complex and structured GUI
applications.
All Swing components are part of the Java Foundation
Classes (JFC).
JFC provides a set of GUI components that simplify the
development of desktop applications.
SWING COMPONENTS JAVA / UNIT - IV
HIERARCHY OF SWING COMPONENTS
HIERARCHY OF SWING COMPONENTS
SWING COMPONENTS JAVA / UNIT - IV
HIERARCHY OF SWING COMPONENTS
HIERARCHY OF SWING COMPONENTS
Component:
The top class in the AWT hierarchy. It is an abstract class that
represents all visual UI elements. It handles events (mouse,
keyboard), size, position, repainting, colors, and fonts.
Container:
The Container class is a Subclass of Component. It can hold and
manage other components. It supports a multilevel containment
system and is responsible for layout management.
Panel:
Panel is a concrete subclass of Container. It is a simple
container without a title bar, menu bar, or border. Components
can be added using add() and positioned using setSize() and
setBounds().
SWING COMPONENTS JAVA / UNIT - IV
HIERARCHY OF SWING COMPONENTS
HIERARCHY OF SWING COMPONENTS
→ Applet: Applet is a small Java program that runs inside a
web browser or applet viewer. It does not have a main()
method and is mainly used to create simple interactive
applications.
Window:
A top-level container placed directly on the desktop.
→
Frame: Subclass of Window. It represents a standard
window with title bar, borders, menu bar, and resizing
options.
→
Dialog: Dialog is a top-level window used to take input
from the user and has a title and border. It inherits the
Window class and does not have minimize or maximize
buttons.
SWING COMPONENTS JAVA / UNIT - IV
HIERARCHY OF SWING COMPONENTS
HIERARCHY OF SWING COMPONENTS
Canvas:
Canvas is a subclass of Component used for drawing graphics in
AWT. It represents a blank rectangular area where we can draw
shapes, images, or custom designs. Although it is not a subclass
of Frame, it is commonly added to frame windows to perform
drawing operations.
SWING COMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
1. Top Level Containers 2. Intermediate Containers
JFrame JDialog JPanel JScrollPane
3. Basic Components 4. Layout Managers
JButton JLabel FlowLayout GridLayout
JTextField JTextArea BorderLayout BoxLayout
JCheckBox JRadioButton GridBagLayout
JComboBox JList
6. Model-View Controller (MVC)
JTable
Model
5. Event View
Hc
A atni o
dnl i Lnig
stene KeyListene Controller
rM o u s e L i s t e n e r
r
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
1. Top Level Containers
→
JFram e
It represents the main
window of a Swing
application.
→ It can contain menus,
toolbars, and other
components.
→
JDialog
It represents a pop-up
dialog or secondary window.
→ It is used for alerts,
prompts, or additional user
input.
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
2. Intermediate Containers
JPanel
→ JPanel is a general-purpose
container used to group and
organize components.
→ It can be added to frames,
dialogs, or other panels to
structure the user interface.
JScrollPane
JScrollPane is a container that
provides scroll bars for
components that are larger than
the visible display area, allowing
users to scroll horizontally and
JCOMPONENTS
vertically. JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
3. Basic Components
JButton: A button that performs an action when clicked.
JLabel: Used to display text or an image.
JTextField: A single-line text field used for user input.
JTextArea: A multi-line text area used for input or displaying
text.
JCheckBox: A checkbox used to select or deselect options.
JRadioButton: A radio button used to select one option from
a group.
JComboBox: A drop-down list used to select an item.
JList: Displays a list of items for selection.
JTable: Displays data in tabular form (rows and columns).
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
3. Basic Components
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
4. Layout Managers
FlowLayout: Arranges components in a row from left to right.
If space is not enough, components move to the next line.
BorderLayout: Divides the container into five regions: NORTH,
SOUTH, EAST, WEST, and CENTER.
GridLayout: Arranges components in a grid of equal-sized
rows and columns.
BoxLayout: Arranges components in a single row (X-axis) or a
single column (Y-axis).
GridBagLayout: A flexible grid-based layout that allows
components to have different sizes and positions.
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
4. Layout Managers
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
5. Event Handling
Swing uses the event delegation model to handle user interactions through
listeners.
ActionListener:
Used to handle action events such as button clicks, menu
selections, and pressing Enter in a text field.
MouseListener:
Used to handle mouse events such as mouse clicks, press,
release, enter, and exit.
KeyListener:
Used to handle keyboard events such as key press, key
release, and key typing.
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
5. Event Handling
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
6. Model-View Controller (MVC)
Swing follows the Model-View-Controller (MVC) architecture pattern.
Model: Represents the data and
business logic of the application.
View: Represents the graphical user
interface (GUI) components and
their layout.
Controller: Handles user input and
manages the interaction between
the Model and the View.
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
JCOMPONENTS JAVA / UNIT - IV
JCOMPONENTS
JCOMPONENTS
JCOMPONENTS JAVA / UNIT - IV
JAVA SWING CONTROLS
JAVA SWING CONTROLS
Swing is a GUI (Graphical User Interface) toolkit used to
create and manage graphical components. Swing provides a
rich set of controls, also known as components or widgets,
that help developers build interactive and user-friendly
applications.
JLabel JTextArea JPasswordField
JRadioButton JButton
ButtonGroup Border
JCheckBox JComboBox
JTextField JTabbedPane
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JLabel:
JLabel is used to display text or an image in a container.
It displays one line of read-only text.
The text can be changed by the program, but the user
cannot edit it directly.
JLabel inherits the JComponent class.
Declaration:
public class JLabel extends JComponent implements SwingConstants,
Accessible
Syntax:
JLabel j1 = new JLabel();
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JLabel Constructors
JLabel() – Creates a label with no text and no image.
JLabel(String s) – Creates a label with the specified text.
JLabel(Icon i) – Creates a label with the specified image.
JLabel(String s, Icon i, int horizontalAlignment) – Creates a
label with text, image, and specified alignment.
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JLabel Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JRadioButton:
JRadioButton is a Swing component that allows the user to
select one option from a group.
Multiple radio buttons are grouped using ButtonGroup, but
only one button can be selected at a time.
Declaration:
public class JRadioButton extends JToggleButton implements Accessible
Syntax:
JRadioButton jrb = new JRadioButton();
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JRadioButton Constructors
JRadioButton() – Creates an unselected radio button with no
text.
JRadioButton(String label) – Creates an unselected radio
button with specified text.
JRadioButton(String label, boolean selected) – Creates a radio
button with specified text and selected status.
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
ButtonGroup:
ButtonGroup is used to group multiple JRadioButton
objects.
It allows the user to select only one option from the group
at a time.
Radio buttons are added using the add() method.
Syntax:
ButtonGroup bg = new ButtonGroup();
[Link](jrb);
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JRadioButton & ButtonGroup Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JCheckBox:
JCheckBox allows the user to select multiple options from a
group.
It is a subclass of JToggleButton.
Declaration:
public class JCheckBox extends JToggleButton implements Accessible
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JCheckBox Constructors
JCheckBox() – Creates an unselected checkbox with no text
or icon.
JCheckBox(String label) – Creates an unselected checkbox
with text.
JCheckBox(String label, boolean selected) – Creates a
checkbox with text and selected status.
JCheckBox(Action a) – Creates a checkbox using properties
from an Action.
SWING CONTROLS JAVA / UNIT - IV
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JCheckBox Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextField
JTextField is used to enter text in a single line.
It allows the user to type and edit text.
It inherits the JTextComponent class.
Declaration:
public class JTextField extends JTextComponent implements SwingConstant
Syntax:
JTextField jtf = new JTextField();
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextField Constructors
JTextField() – Creates a new empty text field.
JTextField(String text) – Creates a text field with specified
text.
JTextField(String text, int columns) – Creates a text field
with specified text and number of columns.
JTextField(int columns) – Creates an empty text field with
specified number of columns.
Page 34
SWING CONTROLS JAVA / UNIT - IV
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextField Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextArea
JTextArea is used to enter text in multiple lines.
It allows the user to type and edit large amounts of text.
It inherits the JTextComponent class.
Declaration:
public class JTextArea extends JTextComponent
Syntax:
JTextArea jta = new JTextArea();
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextArea Constructors
JTextArea() – Creates an empty text area.
JTextArea(String s) – Creates a text area with specified text.
JTextArea(int rows, int columns) – Creates an empty text
area with given rows and columns.
JTextArea(String s, int rows, int columns) – Creates a text
area with specified text, rows, and columns.
SWING CONTROLS JAVA / UNIT - IV
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextArea Example
Page 38
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JButton
JButton is used to perform an action when the user clicks it.
When the button is pressed, it triggers an event in the
program.
It inherits the AbstractButton class.
Declaration:
public class JButton extends AbstractButton implements Accessible
Syntax:
JButton jb = new JButton();
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JButton Constructors
JButton() – Creates a button with no text and no icon.
JButton(String s) – Creates a button with specified text.
JButton(Icon i) – Creates a button with the specified icon.
Page 40
SWING CONTROLS JAVA / UNIT - IV
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTextArea Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
Border
Border is an interface used to apply borders to Swing
components.
Borders are created using the BorderFactory class.
A border is applied to a component using SetBorder() method
[Link](Border);
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
Common BorderFactory Methods:
Border createLineBorder(Color, int): Creates a simple line border with specified
color and thickness.
Border createEtchedBorder(int, Color, Color): Creates a raised or lowered etched
border with highlight and shadow colors.
Border createBevelBorder(int, Color, Color): Creates a raised or lowered bevel
border with specified colors.
MatteBorder createMatteBorder(int, int, int int, Color/Icon): Creates a matte border
with given thickness on each side and color or icon.
TitledBorder createTitledBorder(Border, String): Creates a border with a title
displayed on it.
CompoundBorder createCompoundBorder(Border outer, Border inner): Combines
two borders into one (outer and inner border).
SWING CONTROLS JAVA / UNIT - IV
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JBorder Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JComboBox:
JComboBox displays a list of items in a drop-down menu, and
the user can select one item.
The selected item is shown at the top of the combo box.
It inherits the JComponent class.
Items can be added using the addItem( ) method.
Declaration:
public class JComboBox extends JComponent
implements ItemSelectable, ListDataListener,
ActionListener, Accessible
Syntax:
JComboBox jcb = new JComboBox( );
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JComboBox Constructors
JComboBox( ): Creates a combo box with a default data
model.
JComboBox(Object[ ] items): Creates a combo box with
elements from the specified array.
JComboBox(Vector<?> items): Creates a combo box with
elements from the specified Vector.
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JComboBox Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JPasswordField:
JPasswordField is a text component used for password entry.
It allows editing of a single line of text, but the characters are
hidden (shown as dots or *).
It inherits the JTextField class.
Declaration:
public class JPasswordField extends JTextField
Syntax:
JPasswordField jpf = new JPasswordField( );
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JPasswordField Constructors
JPasswordField(): Creates a password field with default
settings.
JPasswordField(int columns): Creates an empty password
field with the specified number of columns.
JPasswordField(String text): Creates a password field with
initial text.
JPasswordField(String text, int columns): Creates a
password field with specified text and column width.
Page 49
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JPasswordField Example
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTabbedPane:
JTabbedPaJTabbedPane is a container that holds multiple tabs,
and each tab can display a different component.
It allows users to switch between components by clicking tabs
with titles or icons.
Methods to add tabs:
[Link]("TabName", component);
[Link]("TabName", component);
Declaration:
public class JTabbedPane extends JComponent implements
Serializable, Accessible, SwingConstants
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
Syntax:
JTabbedPane jtp = new JTabbedPane();
JTabbedPane Constructors:
JTabbedPane( ): Creates an empty TabbedPane.
JTabbedPane(int tabPlacement): Creates a TabbedPane
with specified tab placement (TOP, BOTTOM, LEFT, RIGHT).
JTabbedPane(int tabPlacement, int tabLayoutPolicy):
Creates a TabbedPane with specified tab placement and
layout policy.
JAVA / UNIT - IV
SWING CONTROLS
JAVA SWING CONTROLS
JAVA SWING CONTROLS
JTabbedPane Example
JAVA / UNIT - IV
SWING CONTROLS
SWING CONTROLS JAVA / UNIT - IV
SWING CONTROLS JAVA / UNIT - IV
SWING CONTROLS
JAVA / UNIT - IV
SWING CONTROLS
JAVA / UNIT - IV
SWING CONTROLS
JAVA / UNIT - IV
SWING CONTROLS
JAVA / UNIT - IV
SWING CONTROLS
JAVA / UNIT - IV
EVENT HANDLING
EVENT HANDLING
Events are used to control the behavior of GUI components.
When a source object (like a button, textbox, etc.) performs an
action, it generates an event.
One or more event listeners receive the event and respond using
event handlers.
Event handlers can be defined three ways:
Using a component’s property sheet
Using a contextual (right-click) menu
Using the Connection Wizard
Events are supported by a number of packages, including [Link],
[Link] and [Link].
EVENT HANDLING JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
The Delegation Event Model (EDM) is the modern approach used in
Java to handle GUI events.
In this model, an event source generates an event and sends it to one
or more event listeners.
The event source is a component such as a button, text field, or menu
that produces an event.
The event listener is an object that waits for the event and processes
it using an event handler method.
A GUI component can delegate (assign) the processing of an event
to another class or method.
Only the registered listeners receive the event notification, making
the system more efficient.
EVENT DELEGATION MODEL (EDM) JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
In the older Java 1.0 event model, events were passed through the
entire component hierarchy until handled..
The Delegation Event Model eliminates this overhead and provides a
cleaner and more efficient way to handle events.
The important parts of this model are Event, Event Source, and Event
Listener.
EVENT DELEGATION MODEL (EDM) JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
1. Events:
An event is an object that represents a change in the state of a
source.
Events are usually generated when a user interacts with GUI
components.
for example,
When a timer expires
When a counter exceeds a value
When a software or hardware failure occurs
When an operation is completed
EDM - EVENTS JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
Event Activities:
Pressing a button
Entering a character using the keyboard
Selecting an item in a list
Clicking the mouse
Page 65
EDM - EVENTS JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
2. Event Sources:
An event source is an object that generates an event when its
internal state changes.
A source can generate more than one type of event.
To receive events, listeners must be registered with the source.
Syntax:
public void addTypeListener(TypeListener el) //add the listener
public void removeTypeListener(TypeListener el) //remove the listener
Type – Name of the event (Example: addKeyListener(), addMouseListener())
el – Reference to the event listener.
EDM - EVENT SOURCES JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
3. Event Listeners:
An event listener is an object that receives notification when an
event occurs.
It has two main requirements:
It must be registered with one or more event sources to receive
event notifications.
It must implement methods to receive and process the events.
These methods are defined in interfaces in the [Link]
package.
Example:
MouseMotionListener interface has methods that handle mouse
movement and mouse dragging events.
EDM - EVENT LISTENERS JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
Event Classes:
→ Classes that represent events in Java are called event classes.
→ The root of the event class hierarchy is EventObject in [Link].
→ It is the superclass of all events.
Syntax:
EventObject(Object src)
src →
the object that generated the event.
Event Class Methods:
getSource() – returns the object that generated the event.
toString() – returns the string representation of the event.
EDM - EVENT LISTENERS JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
AWT Event Class:
→ AWTEvent (in [Link]) is a subclass of EventObject.
It is the superclass of all AWT events used in the delegation
event model.
getID() method identifies the type of event.
→ Action Event generated happen, → Item Event generated happen,
a button is pressed a checkbox is selected or deselected
a menu item is selected an item in a list or choice is selected
a list item is double-clicked Methods:
getItem() – returns the item that
→ Keyboard Event generated changed
when keyboard input occurs. getStateChange() – returns selected
Methods: or deselected state
char getKeyChar()
int getKeyCode() EDM - EVENT LISTENERS JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
→ ItemEvent: Generated when a check box or list item is clicked, or
when a choice or checkable menu item is selected or deselected.
→ KeyEvent: Generated when input is received from the keyboard.
→ MouseEvent: Generated when the mouse is dragged, moved,
clicked, pressed, or released; also when the mouse enters or exits
a component.
→ MouseWheelEvent: Generated when the mouse wheel is moved.
→ TextEvent: Generated when the value of a text field or text area
changes.
EDM - EVENT LISTENERS JAVA / UNIT - IV
EVENT DELEGATION MODEL (EDM)
EVENT DELEGATION MODEL (EDM)
Event Sources:
Source Event Generated Source Event Generated
Button ActionEvent MenuItem ActionEvent / ItemEvent
CheckBox ItemEvent Window WindowEvent
Choice / List ItemEvent ScrollBar AdjustmentEvent
TextField /
TextEvent
TextArea
EDM - EVENT LISTENERS JAVA / UNIT - IV