Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Similar presentations


Presentation on theme: "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."— Presentation transcript:

1 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 2 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

3 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 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

4 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 Event-Based Programming (continued) Figure 9.2: An event “triggers” the initiation of an event object

5 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 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

6 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 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

7 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 Event-Based Programming (continued) Table 9.1: Generic GUI Objects

8 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 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

9 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 The Event-Based Model (continued) When event occurs: –Operating system passes event information to appropriate application –Permits application to take action

10 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 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

11 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 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

12 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 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

13 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 Containment Hierarchy (continued) Figure 9.5: A typical swing-based containment hierarchy

14 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 Containment Hierarchy (continued) Table 9.3: Layout Managers

15 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 Containment Hierarchy (continued) Table 9.5: Lightweight Atomic Components

16 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 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

17 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 Creating a Swing-Based Window (continued) Create JFrame: –JFrame mainFrame = new JFrame("First GUI Window"); Setting size: –mainFrame.setSize(300,150);

18 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 Creating a Swing-Based Window (continued) Figure 9.6: JFrame

19 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 Creating a Swing-Based Window (continued) Display JFrame –Use show() –Or setVisible(true)

20 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 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

21 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 Look and Feel (continued) Table 9.6: Look and Feel GUI Types

22 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 Look and Feel (continued) Figure 9.7: Look and Feel examples

23 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 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

24 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 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

25 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 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

26 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 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

27 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 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

28 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 The Event Delegation Model (continued) Table 9.7: GUI Component Events Types

29 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 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

30 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 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

31 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 Anonymous Classes Class without name Permits placing event handling class code directly into statement that creates instance of event handling class

32 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 Anonymous Classes (continued) Example: mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} } );

33 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 Anonymous Classes (continued) Should only be used when event handler consists of single, short method Always used within statement that creates instance of class

34 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 Anonymous Classes (continued) Figure 9.12: Various registration configurations

35 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 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

36 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 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);

37 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 Adding a Button (continued) Figure 9.13: Button

38 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition38 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");

39 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition39 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”

40 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition40 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

41 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition41 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

42 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition42 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 Swing- based GUI following containment hierarchy Name of listener class for each Swing component must be Java-specified name

43 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition43 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


Download ppt "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."

Similar presentations


Ads by Google