DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Similar documents
DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Introduction. Introduction

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

SINGLE EVENT HANDLING

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED)

GUI Forms and Events, Part II

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Window Interfaces Using Swing Objects

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

EVENTS, EVENT SOURCES AND LISTENERS

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Window Interfaces Using Swing Objects

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

Window Interfaces Using Swing. Chapter 12

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

AP CS Unit 11: Graphics and Events

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

MIT AITI Swing Event Model Lecture 17

Lecture (06) Java Forms

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

More About Objects and Methods

Agenda. Container and Component

Class 16: The Swing Event Model

DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED)

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphical User Interface (GUI)

Java IDE Programming-I

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Assignment 1. Application Development

We are on the GUI fast track path

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

CS 251 Intermediate Programming GUIs: Event Listeners

Programming graphics

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Event Driven Programming

Selection. Chapter 7

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

More About Objects and Methods

Day 2: Review, ICS3U0, June 2017

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Part I: Learn Common Graphics Components

CST141 JavaFX Events and Animation Page 1

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Graphical User Interfaces. Comp 152

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Java Programming Lecture 6

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Graphical User Interfaces (GUIs)

MVC: Model View Controller

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

CS180 Recitation. More about Objects and Methods

CSE 1325 Project Description

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions

Contents Chapter 1 Introduction to Programming and the Java Language

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Swing from A to Z Some Simple Components. Preface

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

CS 106A, Lecture 23 Interactors and GCanvas

Graphical User Interface

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

CP122 CS I. Abstract Interfaces, Graphical User Interfaces, Inheritance

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Starting Out with Java: From Control Structures Through Objects Sixth Edition

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

Graphical User Interface (GUI)

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

CS Exam 1 Review Suggestions

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Contents Introduction 1

Systems Programming Graphical User Interfaces

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Example: Building a Java GUI

CMP 326 Midterm Fall 2015

CS Fall Homework 5 p. 1. CS Homework 5

Example: Building a Java GUI

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Content Area: Mathematics Course: Computer Programming Grade Level: R14 The Seven Cs of Learning

Graphical Applications

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Lab # 02. Basic Elements of C++ _ Part1

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Visit for more.

Java GUI Design: the Basics

CS 170 Java Programming 1. Week 9: Learning about Loops

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Transcription:

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR Chapter Four: ACTION EVENT MODEL 1 Objectives 1 4.1 Additional GUI components 1 JLabel 1 JTextField 1 4.2 Inductive Pause 1 4.4 Events and Interaction 3 Establish an event Model (ActionListener) 3 Register a listener to a GUI object 4 Handle the event when it occurs 4 New GUI components support user interaction 5 4.5 Review Concepts 5 Action Event Model 5 Pattern 6 New methods for JTextField 6 4.6 Challenge 6 4.7 Java Key Words 7 4.8 Exercises 7 Coding Exercises 7 Review Questions 7 Index 9

Chapter Four: ACTION EVENT MODEL Objectives Language components that support user interaction are introduced. The action event model is demonstrated. This model includes implementing an ActionListener, registering a listener, and building an event handler. Additional GUI components JLabel The JLabel is a label to assist users in understanding the GUI and what is expected of them. JTextField This GUI component is like the JTextArea except that it can have only one line of information on it. The JTextField is usually used to collect user input. 4.2 Inductive Pause Enough talk, let s take time out to observe and think. Look at the applet on the following page. Demystifying Programming Ch4: ActionEvent Model 1

Figure 3: ShowName.java Many parts of this applet are familiar; they follow the same patterns that were used in the previous chapter. What do you notice that is different? Which lines contain new elements? What type of object is txtname? On which lines is it referenced? What does the method header on line 41 indicate in its name? (hint: names should be meaningful) What role or purpose is this method performing? On line 42 what do you think is happening with txtname.gettext( )? gettext is a method of class JTextField. If you know the role commonly played by JTextFields, you can probably guess what is happening on line 42. Focus on line 42. What does String username accomplish? What does username accomplish on line 43? Demystifying Programming Ch4: ActionEvent Model 2

4.4 Events and Interaction Solutions become more useful when a user can interact with them. Using the mouse or keyboard a user can supply information or request an action. The program must hear this user action and be programmed to respond in an appropriate manner. Many GUI objects can generate events. Establish an event Model (ActionListener) An event model is usually reflected in the class header. In this chapter we use the ActionListener. The class header for the ShowName class should be: public class ShowName extends JApplet implements ActionListener{ Notice that the ShowName class implements ActionListener. The ActionListener model will assist the programmer in building interactive applets. This event model requires an actionperformed method. This actionperformed method defines the event handler for responding to the enter key being pushed while the prompt is in the JTextField. Carefully explore the following ShowName.java code. It contains many new code items but uses several patterns that were used in the previous chapter. Decide if the line contains new elements. Describe the purpose of each line in your own words. Demystifying Programming Ch4: ActionEvent Model 3

Figure 2: ShowName.java Register a listener to a GUI object The applet must be told to listen to one or more of the GUI components. In the code ShowName applet the JTextField txtname has a listener registered for it. Identify the line that contains a new pattern in the sample after instantiation of the object txtname. Which line registers the listener? Write the code here. The method has one parameter (this) which refers to the applet and its attachment to the browser. The applet listens to any object that has a listener registered for it. When an appropriate event is triggered, the applet gathers information about the event. Handle the event when it occurs Although information has been gathered about an event, a method the event handler is required to respond to the event. The event handler for an action event must be called actionperformed. The header must always be Demystifying Programming Ch4: ActionEvent Model 4

public void actionperformed(actionevent e) The parameter required for this event is always ActionEvent type although it might be called something other than e. For example, the ActionEvent type object might be called evt or george. The code within the curly braces, the method body will always vary depending upon what needs to be done in response to the event. How does the example ShowName.java respond to an ActionEvent? New GUI components support user interaction JLabel Labels are used to provide instructions for users. The text on the face of the label is usually added during instantiation. This GUI object follows the same pattern as the JTextArea. The example provided above has the following code: JLabel lblname; //declared above init( ) //instantiate label and add to Container in body of init lblname = new JLabel("Please enter a name:");//message con.add(lblname); JTextField JTextFields are used to gather information during execution from the user. It follows the same pattern of declaration and instantiation as the JLabel. JTextField txtname; //declared above init( ) //instantiated and added to Container in body of init txtname = new JTextField(15); //size (15) is not required con.add(txtname); 4.5 Review Concepts Action Event Model 1. import the event library 2. implements ActionListener in class header 3. register listener for GUI object 4. create event handler actionperformed( ) Demystifying Programming Ch4: ActionEvent Model 5

Pattern Figure 4: Action Event Pattern The pattern is: 1) import event directory and all its files 2) the class header declares that it implements ActionListener 3) object to be listened to must register a listener in init body 4) an event handler (actionperformed) must be designed to respond to event New methods for JTextField gettext( ): The new method is gettext( ). This method is the opposite of settext( ). As settext writes to the object, gettext reads text from an object. This method must be attached with a dot (.) to an object which has text. String username = txtname.gettext(); Integer.parseInt( ): Another special method may also be useful at times. When a user is asked to type a number in a JTextField, this information can be read using gettext( ). However, this information is text and not a number when it is retrieved from a textfield. If we want to do math with it and if we want to store it in a variable declared for an int (whole number), we ll need to first convert this text into an int. The method uses a wrapper class, Integer and the method parseint: int number = Integer.parseInt( txtusernumber.gettext( ) ); The text typed from the keyboard by the user into the JTextField named txtusernumber is read and parsed (converted) into a whole number. This int is then stored in (assigned to) the newly declared int type variable named number. This method is parseint and is attached to the Integer wrapper class object with a dot (. ) symbol. It is the wrapper class Integer which is parsing the String to int. 4.6 Challenge Is it possible to write to a JLabel or JTextField in the same way that we can to a JTextArea? Prove it to yourself. Demystifying Programming Ch4: ActionEvent Model 6

4.7 Java Key Words implements addactionlistener(this) actionperformed( ) ActionEvent 4.8 Exercises 1. What does the following code accomplish? String myname = txtname.gettext( ); 2. Instantiate each of the GUI objects declared in the following code. JLabel lblfirst, lbllast; JTextField txtfirst, txtlast; JButton btnadd; public void init( ){ } 3. Add each of the GUI objects instantiated in the previous exercise to the Container container. This code would be placed in the init block following the instantiation. Coding Exercises 4. Produce code following the Design Plan from exercise two (2) above. Debug and test your product. 5. Create a JApplet that will receive a user s name and display the name followed by the stated objectives of this chapter. Review Questions 6. An action listener requires an event handler called. Demystifying Programming Ch4: ActionEvent Model 7

7. What library must be imported to support events?. 8. Which of the following lines of code registers a listener? a. btnpress = new JButton( Press ); b. container.add(btnpress); c. btnpress.addactionlistener(this); d. registerlistener(btnpress); Demystifying Programming Ch4: ActionEvent Model 8

Index ActionEvent, 4, 5 ActionListener, 1, 3, 5, 6 actionperformed, 3, 4, 5, 6 assigned, 6 container, 7 event, 1, 3, 4, 5, 6, 7 event handler, 1, 3, 4, 5, 6, 7 event model, 1, 3 events, 3, 7 gettext, 2, 6 GUI, 1, 3, 4, 5, 6, 7 import, 5, 6 instantiate, 5 int, 6 Integer.parseInt, 6 JButton, 7 JLabel, 1, 5, 6 JTextArea, 1, 5, 6 JTextField, 1, 2, 3, 4, 5, 6 methods, 6 new, 2, 3, 4, 5, 6, 7 public, 3, 4, 7 register, 5, 6 String, 2, 6 variable, 6 void, 4, 7 wrapper class, 6 Demystifying Programming Ch4: ActionEvent Model 9