Introducing The Basics of Guis and Event-Handling in Java
Introducing The Basics of Guis and Event-Handling in Java
Introducing The Basics of Guis and Event-Handling in Java
ap7@doc.ic.ac.uk
Introducing the
basics of GUIs and
event-handling in
Java
GUI components
• Buttons
Common
buttons
Radio
buttons
Check
buttons
GUI components
• Combo
boxes
• Lists
• Menus
GUI components
• Spinners
• Sliders
• Textfields
Console vs. GUI applications
• What is the difference between
a GUI and a console app?
JFrame
object Container JButton object
object
JLabel object
Building GUIs essentials
– Example:
JRadioButton rb1= new
JRadioButton(“one”);
JRadioButton rb2= new
JRadioButton(“two”);
ButtonGroup bg= new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
Layout Managers
• You use layout managers
to design your GUIs.
• There are several
managers like:
– FlowLayout
– BorderLayout
– GridLayout
– CardLayout
– GridBagLayout
FlowLayout
• Default layout
• Components laid out from
the top-left corner, from
left to right and top to
bottom like a text.
BorderLayout
– Adding Components:
myCont.add(aComponent, BorderLayout.WEST);
The delegation model
• Sources:
– The mouse and keyboard and
the GUI components (Buttons,
lists, checkboxes etc.)
• Events:
– Objects that describe a state
change in a source.
• Listeners:
– Objects notified when events
occur.
The delegation model
Event
Object
Event Listener
Source
The Source
When
Thethe generates
Source
state of thean
registers event
source
a
and sends
changes… it to the registered
Listener…
listener
Listeners
• Any number of event listener
objects can listen for all kinds
of events from any number of
event source objects.
• E.g. a program might create
one listener per event source.
• Or a program might have a
single listener for all events
from all sources.
Listeners
• Multiple listeners can
register to be notified of
events of a particular type
from a particular source.
• Also, the same listener
can listen to notifications
from different objects.
Listeners
• Each type of listeners can
be notified only for its
corresponding types of
events which can be
generated by specific
types of sources.
Multiple sources, single listener
• Many buttons can register
the same listener since all
buttons generate the same
type of event. This type of
event may be generated by
other types of sources as
ActionListener
well. Act io nEv ent1
n t2
e
button1 n Ev
tio
Ac
t3
button2 en
v
nE
io
t
Ac
ListItem3
Single source, multiple listeners
nt
MouseEve
MouseMotion
Listener
Listeners as interfaces
• You implement an interface
to create a listener.
• In the case of a single source
that generates multiple types
of events you can create a
single listener that
implements all interfaces
(remember: a class may
extend only one superclass
but implement more than one
interfaces).
Single source, multiple listeners again
MouseWheel MouseWheelEvent
MouseWheel +
Listener
MouseMotion
Listener
nt
MouseEve
MouseMotion
Listener
Sources-events-listeners
Source Event Listener Methods
(argument:
<state change> corresponding event)
//Extend a JFrame:
public class MyApp extends JFrame{ ...
b1.addActionListener(new ActionListener(){