Event-Based Programming (continued)
Download
Report
Transcript Event-Based Programming (continued)
Object-Oriented Program Development
Using Java: A Class-Centered Approach,
Enhanced Edition
Objectives
You should be able to describe:
• Event-Based Programming
• Creating a Swing-Based Window
• Adding a Window Closing Event Handler
• Adding a Button Component
• Common Programming Errors
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
2
Event-Based Programming
• Event-based programs
– Provide fully functioning GUI
• Event
– Initiated by user action
– Program must:
• Correctly assess which specific event has occurred
• Provide appropriate code to perform action based on
identified event
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
3
Event-Based Programming
(continued)
Figure 9.2: An event “triggers” the
initiation of an event object
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
4
Event-Based Programming
(continued)
• Actions that trigger events:
– Placing mouse pointer over button and clicking left
mouse button
– Using TAB key until desired button highlighted
with dotted line then pushing Enter key
– Pressing accelerator key
• Sequence of events in program controlled by user
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
5
Event-Based Programming
(continued)
• Programmer provides:
– Code to create GUI
– Code to appropriately process events
• Java provides set of objects for coding GUIs
– AWT
• Older GUI components
– Swing
• Newer GUI components
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
6
Event-Based Programming
(continued)
Table 9.1: Generic GUI Objects
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
7
The Event-Based Model
• Operating system
– Has total control of computer
– Never relinquishes control to any executing
programs
• Most executing programs spend majority of their
time in sleep type of mode
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
8
The Event-Based Model (continued)
• When event occurs:
– Operating system passes event information to
appropriate application
– Permits application to take action
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
9
Containment Hierarchy
• Hierarchy of component placement
• Consists of one and only one top-level container
– Any number of other intermediate containers
– And/or atomic components
• JFrame
– Most commonly used as top-level container
• Heavyweight components
– Responsible for interfacing with operating system
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
10
Containment Hierarchy (continued)
• Content pane
– Internal component provided by each top-level
container
– All visible components displayed by GUI must be
placed on content pane
• Menu bar
– Can also be added to top-level container
– Placed outside of content pane
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
11
Containment Hierarchy (continued)
• Layout manager
– Defines how components are positioned and sized
within container’s content pane
– Default placement can always be changed by
explicitly specifying another layout manager
• Lightweight components
– Intermediate containers and atomic components
– Do not interface with operating system
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
12
Containment Hierarchy (continued)
Figure 9.5: A typical swing-based containment
hierarchy
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
13
Containment Hierarchy (continued)
Table 9.3: Layout Managers
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
14
Containment Hierarchy (continued)
Table 9.5: Lightweight Atomic Components
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
15
Creating a Swing-Based Window
• Two predominant approaches:
– Construct GUI as separate class using Swing
components
– Construct a GUI object using Swing components
from within main() method
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
16
Creating a Swing-Based Window
(continued)
• Create JFrame:
– JFrame mainFrame = new
JFrame("First GUI Window");
• Setting size:
– mainFrame.setSize(300,150);
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
17
Creating a Swing-Based Window
(continued)
Figure 9.6: JFrame
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
18
Creating a Swing-Based Window
(continued)
• Display JFrame
– Use show()
– Or setVisible(true)
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
19
Look and Feel
• Refers to:
– How GUI appears on screen
– How user interacts with it
• Swing package
– Supports four look and feel types
– If no other specified
• Default Java look and feel used
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
20
Look and Feel (continued)
Table 9.6: Look and Feel GUI Types
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
21
Look and Feel (continued)
Figure 9.7: Look and Feel examples
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
22
Adding a Window Closing Event
Handler
• GUI creation process:
– Phase 1: Construct component so that it appears
visually
– Phase 2: Provide event handler for component
• Event handler
– Object that responds appropriately when event
occurs
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
23
The Event Delegation Model
• Requires two basic elements:
– Component to generate event
– Event handler or listener object
• Component delegates responsibility to listener
object for doing something
– When event is generated
• Registration statement
– Glue that attaches event to specific event handler
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
24
The Event Delegation Model
(continued)
• Phase 2: Provide an Event Handler for the
Component
– Step 1: Write code for event handler class
• Known as listener class
– Step 2: Create instance of event handler class
• Means instantiating object of class using new
operator
• Created object known as listener object
– Step 3: Register listener object created in step 2
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
25
The Event Delegation Model
(continued)
• Event handling class coded as separate nonnested
class
• WindowListener interface
– Must be implemented by event handler class
– Required by Java for handling window events
– All listed methods must for interface be
implemented
• Even if they consist of empty bodies
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
26
The Event Delegation Model
(continued)
• Add listener to JFrame:
– mainFrame.addWindowListener(handle
r);
• Multiple listener objects can be registered to same
event source
• Single listener object can be registered to multiple
event sources
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
27
The Event Delegation Model
(continued)
Table 9.7: GUI Component Events Types
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
28
Adapter and Inner Classes
• Adapter classes
– Declare empty event handling methods for given
interface type
– Can be used as parent class for listener class
– Constructed as abstract classes
• Inner class
– One class is nested inside another class
– Place event handler class definition close to other
GUI-related code
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
29
Adapter and Inner Classes (continued)
• Event-handling code guidelines:
– Listener class be made as short as possible
– Code should be placed close as possible to where
object of class is actually instantiated
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
30
Anonymous Classes
• Class without name
• Permits placing event handling class code directly
into statement that creates instance of event
handling class
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
31
Anonymous Classes (continued)
• Example:
mainFrame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
);
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
32
Anonymous Classes (continued)
• Should only be used when event handler consists
of single, short method
• Always used within statement that creates instance
of class
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
33
Anonymous Classes (continued)
Figure 9.12: Various registration configurations
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
34
Adding a Button Component
• Adding components to GUI:
– Phase 1: Construct component so that it appears
visually
• Step 1: Create specific component
• Step 2: Add component into container
– Phase 2: Provide event handler for component
• Step 1: Write code for event handler class
• Step 2: Create instance of event handler class
• Step 3: Register listener object
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
35
Adding a Button
• When adding lightweight components into top-level
container must be added to container’s content pane
• Declare and instantiate JButton:
– private JButton firstButton;
– firstButton = new JButton("Press me");
• Add to main frame’s content pane:
– Container c = mainFrame.getContentPane();
– c.add(firstButton);
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
36
Adding a Button (continued)
Figure 9.13: Button
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
37
Adding ToolTips and Accelerator Keys
• ToolTip
– Single line of text
– Appears when user positions mouse cursor over
GUI component
– Provides quick single-line documentation for
component
– Syntax:
objectName.setToolTipText("string
value");
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
38
Adding ToolTips and Accelerator Keys
(continued)
• Accelerator key
– Called mnemonic key in Java
– Any key that initiates action by pressing Alt key
and designated letter
– Syntax:
objectName.setMnemonic('letter');
– Choose letter contained in object’s caption
• Will be underlined in caption
• Otherwise hot-key will be “hidden”
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
39
Adding an Event Handler
• Event handler for atomic component created and
registered in same way as event handlers for
JFrame container
• ActionListener class is added to JButton
– Must implement actionPerformed() method
– Use addActionListener() method to
register handler
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
40
Common Programming Errors
• Forgetting to include GUI-related import
statements
• Creating event handler and failing to register it
• Modifying GUI class that has previously been
compiled and changing its name but forgetting to
change name when instance of class is created in
main() method
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
41
Summary
• Event-based programs execute program code
depending on what events occur
• Java handles events triggered by GUI components
using event delegation model
• Graphical components structured into Swingbased GUI following containment hierarchy
• Name of listener class for each Swing component
must be Java-specified name
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
42
Summary (continued)
• Each implemented listener class requires specific
set of methods that must be included
• Listener classes can be nested inside class used to
instantiate and display GUI components
• Anonymous class can be used to construct listener
class whenever single-statement listener object
instantiation and registration are employed
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
43