Chapter 5 Processing Input with Applets

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Programming Task: Task 1 Controlled Assessment Practice.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java Applet Basics (2). The Body Mass Index Calculator.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
1 Class 6. 2 Objectives Objectives Identify, declare, and use primitive data types Use the System class to create data streams Use the Scanner class of.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
GUI Programming using Java - Event Handling
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Lecture 09 Applets.
“Form Ever Follows Function” Louis Henri Sullivan
Graphical User Interface (pronounced "gooey")
Section 64 – Manipulating Data Using Methods – Java Swing
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Chapter 4 Defining Instantiable Classes
Introduction to Event Handling
Chapter 7-2 (Book Chapter 14)
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 7-2 (Book Chapter 14)
Tonga Institute of Higher Education
CiS 260: App Dev I Chapter 6: GUI and OOD.
Advanced GUIs and Graphics
Presentation transcript:

Chapter 5 Processing Input with Applets 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 5 Chapter 5 Objectives After you have read and studied this chapter, you should be able to Define an applet with multiple methods. Incorporate a simple event-handling routine to an applet to process input. Construct input-processing applets using Label, TextField, and Button objects from the java.awt package. Convert string data to numerical data. Use the reserved word this in your programs. Run applets without using an applet viewer or browser. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Sample Applet: GreetingApplet The GreetingApplet applet accepts a name entered by the user and replies back with a personalized greeting. This is a text field where the user enters his/her name. The greeting is displayed when the user enters the name and presses the ENTER key. We will develop this applet in two steps: Place GUI components Add an action event handling method 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Template for an Applet Definition import java.applet.*; import java.awt.*; public class extends Applet { } Import Statements javadoc Comment Applet Name Declaration Methods Include a sequence of methods 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

GreetingApplet: Placing GUI Objects Chapter 5 GreetingApplet: Placing GUI Objects public class GreetingApplet extends Applet { private Label prompt; private Label greeting; private TextField inputLine; public GreetingApplet( ) prompt = new Label(“Please enter your name”); greeting = new Label( ); inputLine = new TextField( 15 ); add( prompt ); add( greeting ); add( inputLine ); } Declaration These statements place Label and TextField objects on this applet. To write an input processing applet, you need to know how to define a class with multiple methods. To understand how an applet processes input data, you must learn what events are and how events are handled in Java programs. Actions such as moving a mouse, selecting a menu item, entering data, and pressing the ENTER key are all events. When the user makes one of these actions, we say an event is generated. We program applets so that a designated method will be executed in response to the generated events. A more detailed discussion on event handling is given in Chapter 13. The standard applet classes we discuss in this chapter are from the java.awt and java.applet packages. For the discussion on the Swing version of applets, please visit Dr. Caffeine’s website. Please notice that an understanding of this chapter is required before studying the Swing version.Discussion on the Swing version of applets can be found at www.drcaffeine.com/additionaltopics. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

GreetingApplet: Handling Action Events Chapter 5 GreetingApplet: Handling Action Events We will now add code to process the pressing of the ENTER key. Event sources generate or trigger events and Event listeners process the generated events. For this applet, the TextField object inputLine is the event source and GreetingApplet is the event listener. TextField inputLine generates an action event. In addition to action events, there are mouse events, mouse movement events, item changed events, and so forth. For each different type of event, we have a corresponding event listener. For example, we have action event listeners for action events, mouse motion listeners for mouse movement events, and so forth. Many GUI objects generate some types of events, but not all of them do. For instance, Label objects do not generate events, and therefore, are not event sources. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

GreetingApplet as an Action Event Handler To set a GreetingApplet object be an action event handler, we must Import the Java event-handling classes. Modify the class declaration to include the clause implements ActionListener. Add the actionPerformed method to the class definition. Register the GreetingApplet object to the event source inputLine as its action event listener. (An event source will call the registered listeners’ actionPerformed method when the event occurs.) 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Template for an Action Processing Applet import java.applet.*; import java.awt.*; import java.event.*; public class extends Applet implements ActionListener { } Import Statements javadoc Comment Applet Name Implement Clause Declaration Methods One of which is actionPerformed 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Registering GreetingApplet We register an GreetingApplet object as an action event listener of the event source inputLine by calling inputLine’s addActionListener method. We do this in the constructor: public GreetingApplet { //as before inputLine.addActionListener( this ); } The reserved word this refers to the GreetingApplet object. To refer to an object from within the object’s method, we use the reserved word this. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

The actionPerformed Method Chapter 5 The actionPerformed Method public void actionPerformed( ActionEvent event ) { greeting.setText( “Nice to meet you, “ + inputLine.getText( ) + “.” ); add( greeting ); doLayout( ); } Inside the actionPerformed method, we include the code to process the ENTER key event. Inside the method body, we 1. Fetch the name entered in inputLine by calling its getText method. 2. Concatenate the name returned by the getText method with the message Nice to meet you and the period as "Nice to meet you, " + inputLine.getText() + "." 3. Assign the resulting string from Step 2 to the Label object greeting by calling its setText method. 4. Call the add method to place greeting and call the doLayout method. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

GreetingApplet Object Diagram 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Absolute Positioning of GUI Objects Placement of GUI objects an applet is control by the layout manager. Default layout manager for an applet is FlowLayout. It is possible to set the layout manager to null setLayout( null ); and place GUI objects by specifying their position and size as <object>. setBounds ( x, y, width, height ) ; 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu The setBounds Method inputLine.setBounds( 65, 55, 150, 25 ); 150 25 55 65 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu The Button Class A type of button called pushbutton is one of most ubiquitous GUI objects used today’s window-based programs. We use the Button class from java.awt to create pushbuttons. Button helloButton; helloButton = new Button(“Hello”); A button will generate an action event when it is clicked. Register an action event listener to a button to process the action events. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Converting Text to a Numerical Value A TextField object we to input data from the user will accept data as text value. To input numerical data, we must convert the text value to a numerical value. There are Double, Float, Integer, and others that provide conversion routines. There is also the Convert class in the javabook package that provides a consistent way of converting values. The Convert class uses the Double, Float, Integer, and other standard classes internally to provide conversion services. Please consult its reference and source code. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Converting Text to a Double Value To convert “123.456” to a double value, we write Doulbe doubleObj = new Double(“123.456”); double number = doubleObj.doubleValue(); Alternatively, we can write double number; number = Double.parseDouble(“123.456”); Conversion of a text value to other numerical data types follows the same pattern as above. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Sample Program: Body Mass Index (BMI) Problem Statement Write an applet that displays a BMI of a person given his or her weight in kilograms and height in meters. Major Tasks Get two input values Convert the input text values to double and compute the BMI Display the result Key Formula (given) BMI = w / h2 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu BMIApplet – Design 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Loan Calculator – Object Diagram 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu

Loan Calculator – Development Steps Start with a skeleton BMIApplet class. The skeleton class will include data member declarations and a partially defined constructor that creates and places GUI objects. Add code to the constructor to create and place all GUI objects on the applet. Define the actionPerformed method to process the button click action event. We will include a dummy output statement to verify the button click event is properly detected. Add code to compute and display the BMI and finalize the code. 4/9/2019 Introduction to Object-Oriented Programming with Java--Wu