Chapter 3: GUI Programming in
Java
Arsi University
Department of IS
Course Content
2
Introduction
AWT versus Swing
Classes for representing GUI components
Creating a Window
Adding Frame
Layout Managers
Event Handling
Event Listener
Introduction
3
GUI: interact with the user.
use a graphical window or windows that provide
interaction with the user.
Consists components that present data to the user
and allow interaction with the application.
Buttons, labels, text-fields, check boxes, radio
buttons, combo boxes, sliders, menus etc.
Created by OOL like Java consists of the following
parts:
GUI...
4
Components – Classes that represent elements on
a screen such as a label and a text field.
Containers – Special GUI component classes that
contain other components.
Layout managers-allow a programmer to specify
how components are arranged in a container.
Events–represent user generated events such as
mouse click.
Listeners–receive events from components and act
on them.
Window with GUI components…
5
button menus menu bar combo box
scroll bars
AWT vs. Swing
GUI libraries: to build graphical user interfaces.
AWT is fine for developing simple GUI, but not for developing
comprehensive GUI projects.
Besides, AWT is prone to platform-specific bugs.
AWT user-interface components were replaced by a more robust,
versatile, and flexible library Swing components.
Swing components depend less on the target platform and use less
of the native GUI resource.
Swing components are painted directly on canvases using Java
code.
Swing components don’t rely on native GUI (-lightweight
components), and AWT components are heavyweight components.
6
AWT vs. Swing…
AWT features include:
A rich set of user interface components.
A robust event-handling model.
Graphics and imaging tools, including shape, color, and font classes.
Swing features include:
All the features of AWT.
100% Pure Java certified versions of the existing AWT
component set (Button, Scrollbar, Label, etc.).
A rich set of higher-level components (such as tree view,
list box, and tabbed panes).
Pure Java design, no reliance on peers.
7
Classes for representing GUI components
8
AWT Classes Swing Classes
Description
Label JLabel Displays uneditable text and/or icons
Button JButton Triggers an event when clicked with the mouse
CheckBox JCheckBox Specifies an option that can be selected or not selected
List JList A list of items from which the user can make a selection by clicking on any
one of them. Multiple elements can be selected
Choice JComboBox A drop down list of items from which a user can make a selection
TextField JTextField Allows for the editing of single line text
TextArea JTextArea Allows for the editing of a multiple lines of text
Container A subclass of Component that can hold other components
Panel JPanel An area in which components can be placed and organized
Creating a Window
9
[Link] and [Link] represent a
basic window.
Three steps involved to get a frame window to on the screen.
Instantiate the frame object
Give the frame object size using setSize(), setBounds(), or pack()
Make the frame appear on the screen by invoking setVisible(true)
Creating a frame in AWT
Import the Frame class in the [Link] package into your program
and creating an instance of it.
Creating a Window…
10
• Creating a frame in Swing
• Import JFrame class in the [Link] package
into your program and creating an instance of it.
Creating a Window…
11
Adding components to a container
12
A container exists for displaying components, and
components must be displayed within a container.
Layout Managers
13
Are objects that control the size and position of components
within a container.
Associated with a particular component, always a
background component.
Controls the components contained within the component
the layout manager is associated with.
Swing and AWT containers use the same set of layout
managers, which are mostly AWT objects.
AWT has five layout managers
Layout Managers…
14
BorderLayout: divides a background component into five
regions: North, South, East, West and Center.
FlowLayout: adds components in a left-to-right flow, with
each component given its preferred size.
GridLayout: divides a container into grid of rows and
columns.
Using Menus in java
15
Menu provides a space saving way to let the user choose one
of several options.
To provide menus, a top level window (a frame) should have
a menu bar associated with it.
A menu bar displays a list of top level menu choices.
Implemented in the swing package with by: JMenuBar,
JMenu, and JMenuItem.
JMenuBar contains one or more JMenu objects.
Using Menus in java…
16
Each JMenu object contains a list of JMenuItem
objects.
Each JMenuItem object represents something that
can be selected by the user.
To add the menus onto a menu bar and menu items
onto a menu, use the add() method.
To add the menu bar to a frame, call the
setJMenuBar() method on the frame object.
Event Handling
17
Mechanism that controls the event and decides what should
happen if an event occurs.
This mechanism has the code - event handler that is
executed when an event occurs.
Java uses a mechanism- “Delegation Event” Model to handle
different events.
Defines mechanism based on the concept that for every
event, there is an event source and an event listener.
Event Handling…
18
There are 3 major players in the Delegation Event Model:
1. Event Object: describes the event that has occurred.
Contains all necessary information(its type, its source and
when it occurred.
In java every event is represented by a class.
Instance of event class is created when an event that is
represented by that class occurs.
Event Handling…
19
2. Event Source: is an object that generates an event.
An event is generated when an object changes its state in
some way.
May generate more than one type of event.
A source must register listeners, listeners can receive
notifications about a specific type of event.
Each type of event has its own registration method
Event Handling…
20
The general form of registering listeners is:
public void addTypeListener(TypeListener e)
Type- name of event, and e- reference to event listener.
Example:
method registers a keyboard event listener
addKeyListener().
Method registers a mouse motion listener is
addMouseMotionListener().
The general form of removing a registered listener is:
public void removeTypeListener(TypeListener e)
Event Handling…
21
3. Event Listener: an object that is notified when an event
occurs.
It has two major requirements.
It must have registered with one or more event sources to
receive notifications about specific types of events.
It must implement methods to receive and process those
notifications.
Event Handling…
22
Event Class: are represented in java using classes.
Event Handling…
Event Classes, Listener Interfaces
23 & Listener Methods
Event Handling…
24
Handling Mouse Events: can be generated by any GUI
component.
Tohandle mouse events, you need to implement the
MouseListener or MouseMotionListener interfaces.
Each mouse event-handling methods receives as an argument
a MouseEvent object that contains information about the
mouse event.
Event Handling…
25
Handling Key Events: Key events are generated when keys
on the keyboard are pressed and released.
A class that implements KeyListener must provide declarations
for methods keyPressed, keyReleased and keyTyped, each of
which receives a KeyEvent as its argument.
keyPressed is called in response to pressing any key.
keyTyped is called in response to pressing any key that is not
an action key.
(The action keys are any arrow key, Home, End, Page
Up,Page Down, any function key, etc.)
keyReleased is called when the key is released after any
keyPressed or keyTyped event.
Thank You!!!
26
Lab Continued…