Chapter 7 Improving the User Interface

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Chapter 8 Improving the User Interface
Chapter 3 Syntax, Errors, and Debugging
Chapter 2 First Java Programs
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Chapter 10 Classes Continued
Chapter 14: Event-Driven Programming with Graphical User Interfaces
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 2 First Java Programs
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
A First Program Using C#
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Lesson 7: Improving the User Interface
1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Java Programming, Second Edition Chapter Five Input and Selection.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Java Programming: Guided Learning with Early Objects
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lecture 2 Objectives Learn about objects and reference variables.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 4 Introduction to Control Statements
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Chapter 2 First Java Programs Fundamentals of Java.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Introduction to Programming and Visual Basic
Chapter 3 Syntax, Errors, and Debugging
“Form Ever Follows Function” Louis Henri Sullivan
Objectives You should be able to describe: Interactive Keyboard Input
Event loops 16-Jun-18.
Chapter 2 First Java Programs
Chapter 14: Exception Handling
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Event loops.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Event loops 8-Apr-19.
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Chapter 7 Improving the User Interface Fundamentals of Java

Objectives Construct a query-driven terminal interface. Construct a menu-driven terminal interface. Construct a graphical user interface. Format text, including numbers, for output. Handle number format exceptions during input. Fundamentals of Java

Vocabulary Menu-driven program Query-controlled input Fundamentals of Java

A Thermometer Class Fundamentals of Java

Repeating Sets of Inputs Chapter 4 presented count-controlled and sentinel-controlled input. Query-controlled input: Before each set of inputs after the first, the program asks the user if there are more inputs. Fundamentals of Java

Repeating Sets of Inputs (cont.) Figure 7-1: Interface for a query-controlled temperature conversion program Fundamentals of Java

Repeating Sets of Inputs (cont.) Program implemented by two classes: Interface class Thermometer class Pseudocode for interface class: Fundamentals of Java

Repeating Sets of Inputs (cont.) Example 7.1: ConvertWithQuery.java Fundamentals of Java

Menu-Driven Conversion Program Menu-driven programs: Display list of options; user selects one Program prompts for additional inputs related to that option and performs computations. Menu is displayed again. Fundamentals of Java

Menu-Driven Conversion Program (cont.) Figure 7-2: Interface for a menu-driven version of the temperature conversion program Fundamentals of Java

Menu-Driven Conversion Program (cont.) Pseudocode: Fundamentals of Java

Formatted Output with printf and format Java 5.0 includes method printf for formatting output. Requires format string and data values General form of printf: Fundamentals of Java

Formatted Output with printf and format (cont.) Format string is a combination of literal string information and formatting information. Formatting information consists of one or more format specifiers. Begin with a ‘%’ character and end with a letter that indicates the format type Fundamentals of Java

Formatted Output with printf and format (cont.) Table 7-1: Commonly used format types Fundamentals of Java

Formatted Output with printf and format (cont.) Symbol %n embeds an end-of-line character in a format string. Symbol %% produces literal '%' character. When compiler sees a format specifier, it attempts to match that specifier to an expression following the string. Must match in type and position Fundamentals of Java

Formatted Output with printf and format (cont.) printf can justify text and produce tabular output. Format flags support justification and other styles. Table 7-2: Some commonly used format flags Fundamentals of Java

Formatted Output with printf and format (cont.) Figure 7-3: Table of sales figures shown with and without formatting Fundamentals of Java

Formatted Output with printf and format (cont.) To output data in formatted columns: Establish the width of each field. Choose appropriate format flags and format specifiers to use with printf. Width of a field that contains a double appears before the decimal point in the format specifier f Fundamentals of Java

Formatted Output with printf and format (cont.) Table 7-3: Some example format strings and their outputs Fundamentals of Java

Formatted Output with printf and format (cont.) Example 7.3: Display a table of names and salaries Fundamentals of Java

Formatted Output with printf and format (cont.) Formatting with String.format: Can be used to build a formatted string Same syntax as printf Difference is that resulting string is not displayed on the console Fundamentals of Java

Handling Number Format Exceptions During Input If data found to be invalid after input, the program can display an error message Prompts for the data again Must detect and handle when a number is requested from the user, but the user enters a non-numerical value e.g., during a Scanner.parseInt statement Fundamentals of Java

Handling Number Format Exceptions During Input (cont.) The try-catch construct allows exceptions to be caught and handled appropriately. Statements within try clause executed until an exception is thrown Exceptions sent immediately to catch clause Skipping remainder of code in try clause Fundamentals of Java

Handling Number Format Exceptions During Input (cont.) If no statement throws an exception within the try clause, the catch clause is skipped. Many types of exceptions can be thrown. Catching an Exception object will catch them all. Fundamentals of Java

Graphics and GUIs GUIs based on pop-up dialogs can be limiting. Static and rigid Better GUI presents the user with entry fields for many of the data values simultaneously Offer many command options Via buttons, drop-down lists, and editable fields Fundamentals of Java

The Model/View/Controller Pattern Figure 7-4: Interface for the GUI-based temperature conversion program Fundamentals of Java

The Model/View/Controller Pattern (cont.) Create classes to represent: Model: The data that the program uses and the operations that can be performed on that data View: What the user of the program interacts with Controller: Coordinates model and view classes by passing messages and data between them Listener classes Can be attached to widgets in the view class Fundamentals of Java

The Model/View/Controller Pattern (cont.) When a controller class receives an event from a view class, it sends a message to a model class. Use a separate class to set up model, view, and controller classes from within a main method. The application Fundamentals of Java

The Model/View/Controller Pattern (cont.) Temperature conversion application class: Fundamentals of Java

The Model/View/Controller Pattern (cont.) GUIWindow is the main view class Instantiates and maintains reference to the data model A Thermometer Instantiates and maintains references to data fields and the command button Adds widgets to window’s container Instantiates and attaches a FarhenheitListener to the command button Fundamentals of Java

The Model/View/Controller Pattern (cont.) Example 7.5: GUIWindow.java Fundamentals of Java

The Model/View/Controller Pattern (cont.) Example 7.5: GUIWindow.java (cont.) Fundamentals of Java

The Model/View/Controller Pattern (cont.) Example 7.5: GUIWindow.java (cont.) Fundamentals of Java

The Model/View/Controller Pattern (cont.) Table 7-4: Some JTextField methods Fundamentals of Java

The Model/View/Controller Pattern (cont.) Deciding the layout of the view class(es) is very important. Often necessary to break view into smaller pieces (panels) Each formatted separately Add formatted panels to other panels to build the layout. Fundamentals of Java

The Model/View/Controller Pattern (cont.) Real GUI programs are event-driven. When program opens, it waits for events Events are generated when the user interacts with the GUI. Events invoke controller classes, which in turn invoke model classes. Fundamentals of Java

Making Temperature Conversion Go Both Ways Figure 7-5: Temperature converter that goes both ways Fundamentals of Java

Making Temperature Conversion Go Both Ways (cont.) Alterations: Declares and instantiates second button Adds button to button panel Creates listener object and attaches it to button Defines a separate listener class that converts from Celsius to Fahrenheit Fundamentals of Java

Making Temperature Conversion Go Both Ways (cont.) Example 7.6: Listener to convert Celsius to Fahrenheit Fundamentals of Java

Making the Temperature Conversion Robust Example 7.7: Robust listener for number format errors Fundamentals of Java

Making the Temperature Conversion Robust (cont.) Figure 7-6: Responding to a number format error Fundamentals of Java

Summary The terminal input/output (I/O) interface can be extended to handle repeated sets of inputs. Query-based pattern Menu-driven pattern The graphical user interface (GUI) allows user to interact with a program by displaying window objects and handling mouse events. Fundamentals of Java

Summary (cont.) Terminal-based program: Program controls most of the interaction with the user GUI-based program: Driven by user events Two primary tasks of a GUI-based program: Arrange window objects Handle user interactions Fundamentals of Java