Object Oriented Programming
CSOO-122
Presented by
Dr. Muhammad Touseef Irshad
Lecture # 15
Department of Computer Science 1
National University of Modern Languages, Peshawar Campus
Graphical User Interface
▪ A Graphical User Interface (GUI) is a user-friendly
interface that includes buttons, text fields, labels,
checkboxes, windows, etc., allowing users to
interact with software easily.
▪ These components are part of Java's Abstract
Window Toolkit (AWT) and Swing libraries.
2
Graphical User Interface
▪ Graphical User Interface (GUI) components refer to
the visual elements that allow users to interact
with a Java application using graphics rather than
just text-based commands.
▪ AWT (Abstract Window Toolkit) is Older, platform.
▪ Swing, newer and more flexible.
3
Introduction to Swing in Java
▪ Swing is a part of Java’s javax.swing package that
allows developers to create graphical user
interfaces (GUIs).
▪ It is more powerful and flexible than AWT and is
platform independent.
▪ Swing is written entirely in Java and does not rely
on the native OS GUI components.
4
Introduction to Swing in Java
Common Swing Components
Sno Component Class Name Description
1 Window/Frame JFrame A window with title bar and borders.
2 Label JLabel Displays a line of text.
3 Button JButton A clickable button.
4 Text Field JTextField Allows single-line text input.
5 Text Area JTextArea Allows multi-line text input.
6 Password Field JPasswordField A text field that hides typed characters.
7 Checkbox JCheckBox Toggle between selected/unselected.
8 Radio Button JRadioButton Allows single selection in a group.
9 Combo Box JComboBox Drop-down menu.
10 Panel JPanel Container for organizing components.
5
Introduction to Swing in Java
Frame or Window
▪ In Java Swing, a window or frame is a top-level
container that represents the main window of a
GUI application.
▪ A frame is an object of the class jframe.
▪ It serves as the main window where you can place
and organize other GUI components like buttons,
text fields, labels, etc. 6
Sample Code1 (Frame)
7
Sample Code (Frame)
8
Introduction to Swing in Java
▪ Other values for setDefaultCloseOperation ()
9
Sample Code2 (Frame Icon)
Non primitive Data Type
Variable
Abstract Class of AWT
Static Method
Method Chain
10
Sample Code2 (Frame Icon)
11
Sample Code3 (Frame Background Colour)
Gets the main area of the frame
12
Sample Code3 (Frame background Colour)
13
Introduction to Swing in Java
Button
▪ In Java Swing, a button is a GUI (Graphical User
Interface) component that the user can click to
perform an actions, like submitting a form, closing
a window, or starting a task.
▪ javax.swing.JButton
14
Sample Code1 (Button)
arranges components (like buttons) left to right, in a row
and respects their preferred size
Suggests a size for the button:
100 pixels wide and 40 pixels high.
Sets a black border around the
button
Automatically arranges left to right
15
Sample Code1 (Button)
16
Sample Code2 (Button)
Manual placement
X,Y and Width, Hight
17
Introduction to Swing in Java
Label
▪ A label is a non editable GUI component used to
display text or an image to the user.
▪ It is created using the JLabel class from the
javax.swing package
18
Sample Code1 (Label)
19
Sample Code1 (Label)
20
Sample Code1 (Label)
21
Sample Code2 (Label)
22
Sample Code2 (Label)
23
Introduction to Swing in Java
Text Field
▪ Text field is a text box that allows the user to type a
single line of text (like name, email, etc.).
▪ It is created using the JLabel class from the
javax.swing package
24
Sample Code1 (Text Field)
25
Sample Code1 (Text Field)
26
Introduction to Swing in Java
Text Area
▪ is a multi-line text box in Java Swing.
It lets the user type or display multiple lines of
text.
▪ It is created using the JTextArea class from the
javax.swing package
27
Sample Code1 (Text Area)
28
Sample Code1 (Text Area)
29
Introduction to Swing in Java
Radio Button
▪ Radio Button is a GUI component that lets the user
select only one option from a group (like gender:
Male / Female).
▪ It is created using the JRadioButton class from the
javax.swing package
30
Sample Code (Radio Button)
31
Sample Code (Radio Button)
32
Introduction to Swing in Java
Check Box
▪ A checkbox allows the user to select or unselect an
option.
▪ Multiple checkboxes can be selected at the same.
time.
▪ It is created using the JCheckBox class from the
javax.swing package
33
Sample Code (Check Box)
34
Sample Code (Check Box)
35
Introduction to Swing in Java
Menu
▪ A menu is a set of options (like File, Edit, Help)
usually placed on the top of a window.
▪ Java uses Swing menus:
i. JMenuBar (the top menu bar)
ii. JMenu (a drop-down menu like “File”)
iii. JMenuItem (options inside a menu like "Open",
"Save“)
36
Sample Code (Menu)
37
Sample Code (Menu)
38
Introduction to Swing in Java
Event Handling
▪ Event Handling means responding to user actions
like:
i. Clicking a button
ii. Pressing a key
iii. Moving or clicking the mouse
iv. Closing a window
39
Introduction to Swing in Java
▪ Two main concepts are associated with Event
Handling and GUI programming:
▪ Event Listener
▪ Event Action
40
Introduction to Swing in Java
What is Event Listener?
▪ A listener is an object that "listens" for an event
and performs some action when the event occurs.
41
Introduction to Swing in Java
What is Event Action?
▪ The action is the code that runs in response to the
event.
Example: When a button is clicked → background
color changes → this is the action.
42
Sample Code1 (Event Handling)
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.getContentPane().setBackground(Color.PINK);
}
}); This is a lambda expression e is the
event object
X,Y,W,H
43
Sample Code1 (Event)
44