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

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Introduction to visual programming C#. Learning Outcomes In this chapter, you will learn about :  Event-Based Programming  The Event Based Model  Application.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
1 Lecture 8: User Interface Components with Swing.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
CompSci 230 S Programming Techniques
Provision for GUIs in Java
Processing Timer Events
Provision for GUIs in Java
Ellen Walker Hiram College
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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);

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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);} } );

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

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

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

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);

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

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");

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”

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

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

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

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