Professional Documents
Culture Documents
1 Compiled By Mikiyas D.
Outline
Introduction
AWT Vs Swing
GUI Programming using AWT, Swing and JavaFX
Event Handling in Java
2 Compiled By Mikiyas D.
Introduction to GUI
In GUI, users interact with a set of visual controls (buttons, labels,
text boxes, tool bars, menu items) to make an application do its
required tasks.
Java GUI applications are event-driven, meaning nothing happens until
an application is called upon to respond to some event (button pressing,
menu selection,...).
GUI applications are governed by event listeners –they “listen” for
events to occur. Nothing happens until an event is detected.
3 Compiled By Mikiyas D.
Cont’d……..
The fundamental elements need to create a Java GUI
application will come from two packages: the Abstract
Windowing Toolkit (AWT) and Swing.
When Java was first released in 1995, it contained a GUI API
referred to as the Abstract Windowing Toolkit (AWT).
This API contained classes like Frame to represent a typical
window, Button to represent buttons, Menu to represent a
window’s menu, and so on.
The classes and interfaces of the AWT are in the java.awt
packages.
4 Compiled By Mikiyas D.
Abstract Windowing Toolkit (AWT)
AWT (Abstract Window Toolkit) is an API to develop Graphical User
Interface (GUI) or windows-based applications in 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.
5 Compiled By Mikiyas D.
Java AWT Hierarchy
6 Compiled By Mikiyas D.
Useful Methods of Component Class
Here are some useful methods of the component class:
7 Compiled By Mikiyas D.
Swing
Swing is a part of Java Foundation Classes (JFC) that is used to
create window-based applications.
It is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in java.
Unlike AWT, Java Swing provides platform-independent and
lightweight components.
The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
8 Compiled By Mikiyas D.
Java Swing Hierarchy
9 Compiled By Mikiyas D.
AWT Vs Swing
AWT Swing
AWT components are platform- Java swing components
dependent. are platform-independent.
AWT components are Swing components are lightweight.
heavyweight. Swing supports pluggable look
AWT doesn't support pluggable and feel.
look and feel. Swing provides more powerful
AWT provides less components such as tables, lists,
components than Swing. scrolllpanes, colorchooser,
tabbedpane etc.
10 Compiled By Mikiyas D.
AWT Example
To create simple AWT example, you need a frame. There are two
ways to create a GUI using Frame in AWT.
11 Compiled By Mikiyas D.
AWT Frame Demo by Inheritance
12 Compiled By Mikiyas D.
AWT Frame Demo by Association
13 Compiled By Mikiyas D.
Some AWT Components
14 Compiled By Mikiyas D.
AWT Button
Some Constructors
Some Methods
15 Compiled By Mikiyas D.
AWT Button Demo
16 Compiled By Mikiyas D.
AWT Label
Some Constructors
Some Methods
17 Compiled By Mikiyas D.
AWT Label Demo
18 Compiled By Mikiyas D.
AWT TextField
Some Constructors
Some Methods
19 Compiled By Mikiyas D.
AWT TextField Demo
20 Compiled By Mikiyas D.
AWT TextArea Demo
21 Compiled By Mikiyas D.
AWT Checkbox
Some Constructors
Some Methods
22 Compiled By Mikiyas D.
AWT Checkbox Demo
23 Compiled By Mikiyas D.
AWT CheckboxGroup Demo
24 Compiled By Mikiyas D.
AWT Choice
Constructor used
Some Methods
25 Compiled By Mikiyas D.
AWT Choice Demo
26 Compiled By Mikiyas D.
AWT Menu, MenuItem, MenuBar
Some Constructors
Some Methods
27 Compiled By Mikiyas D.
AWT Menu, MenuItem, MenuBar Demo
28 Compiled By Mikiyas D.
Java Swing Components
29 Compiled By Mikiyas D.
JButton
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed
Some Constructors
Some Methods
30 Compiled By Mikiyas D.
JButton Demo
31 Compiled By Mikiyas D.
JLabel
It is used to display a single line of read only text. The text can be changed by an
application but a user cannot edit it directly. It inherits JComponent class.
32 Compiled By Mikiyas D.
Jlabel Demo
33 Compiled By Mikiyas D.
JTextField
JTextField class is a text component that allows the editing of a single line text. It
inherits JTextComponent class.
34 Compiled By Mikiyas D.
JTextField Demo
35 Compiled By Mikiyas D.
JPasswordField
JPasswordField class is a text component specialized for password entry. It allows
the editing of a single line of text. It inherits JTextField class
36 Compiled By Mikiyas D.
JPasswordField Demo
37 Compiled By Mikiyas D.
Login Form Demo
38 Compiled By Mikiyas D.
JTextArea
JTextArea class is a multi line region that displays text. It allows the editing of
multiple line text. It inherits JTextComponent class
39 Compiled By Mikiyas D.
JTextArea Demo
40 Compiled By Mikiyas D.
JCheckBox
JCheckBox 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 ".It inherits JToggleButton class.
41 Compiled By Mikiyas D.
JCheckBox Demo
42 Compiled By Mikiyas D.
JRadioButton
JRadioButton class is used to create a radio button. It is used to choose one option
from multiple options. It is widely used in exam systems or quiz.
43 Compiled By Mikiyas D.
JRadioButton Demo
44 Compiled By Mikiyas D.
JChomboBox
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 Jcomponent class.
Commonly Used Constructors
45 Compiled By Mikiyas D.
JComboBox Demo
46 Compiled By Mikiyas D.
JMenuBar, JMenu and JMenuItem
JMenuBar class is used to display menubar on the window
or frame. It may have several menus.
JMenu class is a pull down menu component which is
displayed from the menu bar. It inherits the JMenuItem
class.
JMenuItem class adds a simple labeled menu item. The
items used in a menu must belong to the JMenuItem or
any of its subclass.
47 Compiled By Mikiyas D.
JMenuBar, JMenu and JMenuItem Demo
48 Compiled By Mikiyas D.
QUIZE (5 %)
Write a Swing program that Displays the Registration
Form shown in the figure below?
49 Compiled By Mikiyas D.
JTable
JTable class is used to display data in tabular form. It is composed of rows
and columns
Commonly Used Constructors
50 Compiled By Mikiyas D.
JTable Demo
51 Compiled By Mikiyas D.
JScrollBar
JScrollBar class is used to add horizontal and vertical scrollbar. It is an
implementation of a scrollbar. It inherits JComponent class.
Commonly Used Constructors
52 Compiled By Mikiyas D.
JScrollBar Demo
53 Compiled By Mikiyas D.
JOptionPane
JOptionPane class is used to provide standard dialog boxes such as message dialog
box, confirm dialog box and input dialog box.
These dialog boxes are used to display information or get input from the user.
54 Compiled By Mikiyas D.
Cont’d…….
Commonly Used Methods
55 Compiled By Mikiyas D.
JOptionPane Demo
m
56 Compiled By Mikiyas D.
JOptionPane Demo
57 Compiled By Mikiyas D.
JPanel
JPanel is a simplest container class. It provides space in which an application can
attach any other component. It inherits the JComponents class.
58 Compiled By Mikiyas D.
JPanel Demo
59 Compiled By Mikiyas D.
JSlider
JSlider class is used to create the slider. By using JSlider, a user can select a value
from a specific range.
Commonly Used Constructors
60 Compiled By Mikiyas D.
JSlider Demo
61 Compiled By Mikiyas D.
Java LayoutManagers
62 Compiled By Mikiyas D.
LayoutManagers
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.
Most Commonly Used LayoutManagers
FlowLayout
BorderLayout
GridLayout
63 Compiled By Mikiyas D.
FlowLayout
It is the default layout of the applet or panel.
FlowLayout class is used to arrange the components in a line, one after another (in a
flow)
Normally all components are set to one row, according to the order of different
components.
If all components can not be fit into one row, it will start a new row and fit the rest in.
64 Compiled By Mikiyas D.
FlowLayout Demo
65 Compiled By Mikiyas D.
BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south,
east, west, and center
66 Compiled By Mikiyas D.
BorderLayout Demo
67 Compiled By Mikiyas D.
GridLayout
GridLayout class is used to arrange the components in a rectangular grid.
One component is displayed in each rectangle.
it is used to lay out the components in a rectangle grid, which has been
divided into equal-sized rectangles and one component is placed in each
rectangle
Commonly Used Constructors
68 Compiled By Mikiyas D.
GridLayout Demo
69 Compiled By Mikiyas D.
Java Event Handling
70 Compiled By Mikiyas D.
What is an Event?
Change in the state of an object is known as event i.e. event describes
the change in state of source
Events are generated as result of user interaction with the graphical
user interface components
For example, clicking on a button, moving the mouse, entering a
character through keyboard, selecting an item from list, scrolling the
page are the activities that causes an event to happen.
Event Handling is the mechanism that controls the event and decides
what should happen if an event occurs.
This mechanism have the code which is known as event handler that is
executed when an event occurs.
71 Compiled By Mikiyas D.
Cont’d ………
Java Uses the Delegation Event Model to handle the events.
This model defines the standard mechanism to generate and handle the events.
The Delegation Event Model has the following key participants namely:
for providing information of the occurred event to it's handler. Java provide as
listener is also an object. Listener waits until it receives an event. Once the
72 Compiled By Mikiyas D.
How Delegation Event Model Works?
It defines a standardized and compatible mechanism for generating and processing
events
In this approach, a source generates an event and sends it to one or more listeners.
The user interface elements can delegate the processing of an event to a different
function.
73 Compiled By Mikiyas D.
Some Commonly Used Events
74 Compiled By Mikiyas D.
Java Action Event Handling Demo
75 Compiled By Mikiyas D.
Output
Before Clicking Button After Clicking Button
76 Compiled By Mikiyas D.
Handling Multiple Action Event Demo
77 Compiled By Mikiyas D.
Cont’d………
78 Compiled By Mikiyas D.
Cont’d………
79 Compiled By Mikiyas D.
Output
Before Clicking Any Button After Clicking + Button
80 Compiled By Mikiyas D.
Java ItemEvent Handling
Java ItemListener is notified whenever you click on the
checkbox.
It is notified against ItemEvent.
The ItemListener interface is found in java.awt.event
package
It has only one method: itemStateChanged().
The itemStateChanged() method is invoked automatically
whenever you click or unclick on the registered checkbox
component
81 Compiled By Mikiyas D.
Java ItemEvent Handling Demo
82 Compiled By Mikiyas D.
Cont’d……..
83 Compiled By Mikiyas D.
Output
Before Checking any CheckBox After Checking Orange CheckBox
84 Compiled By Mikiyas D.
Java KeyEvent Handling
Java KeyListener is notified whenever you change the state of key.
It is notified against KeyEvent.
The KeyListener interface is found in java.awt.event package, and it has
three methods.
Methods of KeyListener interface
85 Compiled By Mikiyas D.
Java KeyEvent Handling Demo
86 Compiled By Mikiyas D.
Cont’d……….
87 Compiled By Mikiyas D.
Output
Before Any Item Event After Releasing Keys
88 Compiled By Mikiyas D.
89
?
Compiled By Mikiyas D.