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.

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

Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Java Programming Chapter 10 Graphical User Interfaces.
Applets Java API.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
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, 4e Chapter 12 Advanced GUIs and Graphics.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Java Programming: Guided Learning with Early Objects
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
– 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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Layout Managers Arranges and lays out the GUI components on a container.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
GUI Programming Joseph Sant Sheridan College. Agenda Elements of GUI programming Component-Based Programming. Event-Based Programming. A process using.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
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.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©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.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Advanced User Interfaces
Java Programming: From Problem Analysis to Program Design,
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Events, Event Handlers, and Threads
Tonga Institute of Higher Education
Chapter 5 Processing Input with Applets
Presentation transcript:

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 or occurrence, not part of a program, detected by the program. Events can be user actions--clicking a mouse button, pressing a key, hovering mouse over text box user actions--clicking a mouse button, pressing a key, hovering mouse over text box system occurrences—running out of memory, raising exceptions system occurrences—running out of memory, raising exceptions Event-driven program—responds to events, such as most programs in Windows environment Event-driven program—responds to events, such as most programs in Windows environment

Terminology (cont.) Event Listener— an interface (like a class, but provides only methods)—that will detect an event. The interface provides only an outline of the methods, and the programmer must write the code for each method. Event Listener— an interface (like a class, but provides only methods)—that will detect an event. The interface provides only an outline of the methods, and the programmer must write the code for each method. Event Handler— code that is executed when an even is detected Event Handler— code that is executed when an even is detected

To Make Interactive Program Steps to make a program interactive:. Steps to make a program interactive:. 1.Identify components with anticipated events (e.g., a button with a button- click) 2.Associate event listeners with the components 3.Write event handlers

Types of Events Action event Action event occurs when a button, check box, radio button, or text field is clicked. occurs when a button, check box, radio button, or text field is clicked. Adjustment event Adjustment event occurs occur when a scrollbar is adjusted by dragging an arrow on the bar or clicking anywhere on the bar. occurs occur when a scrollbar is adjusted by dragging an arrow on the bar or clicking anywhere on the bar. Focus event Focus event occurs when component gains or loses focus on the GUI. E.g., a text field can gain a focus by clicking the mouse anywhere inside it or by pressing the tab key occurs when component gains or loses focus on the GUI. E.g., a text field can gain a focus by clicking the mouse anywhere inside it or by pressing the tab key

Types of Events (cont.) Key event Key event occurs when a particular key is pressed on the keyboard. An event listener can distinguish each key so that appropriate event handlers can be provided. occurs when a particular key is pressed on the keyboard. An event listener can distinguish each key so that appropriate event handlers can be provided. Mouse event Mouse event occurs when the mouse is clicked over a component, when it hovers over a component, or when it leaves the component's area. occurs when the mouse is clicked over a component, when it hovers over a component, or when it leaves the component's area.

Event Listener Event listener—an interface corresponding to each type of event. Event listener—an interface corresponding to each type of event. When an event listener is associated with a class, the class must implement all methods in the event listener. When an event listener is associated with a class, the class must implement all methods in the event listener.

Event Listener (cont.) Interface ActionListener contains method actionPerformed(), to handle action events. Interface ActionListener contains method actionPerformed(), to handle action events. Interface AdjustmentListener contains method adjustmentValueChanged() to handle the adjustment events. Interface AdjustmentListener contains method adjustmentValueChanged() to handle the adjustment events. Interface FocusListener contains two methods: focusGained() and focusLost() to handle focus events. Interface FocusListener contains two methods: focusGained() and focusLost() to handle focus events. Interface KeyListener contains three methods: keyPressed(), keyReleased(), and keyTyped(), to handle key events. Interface KeyListener contains three methods: keyPressed(), keyReleased(), and keyTyped(), to handle key events. Interface MouseListener contains five methods: moustClicked(), mouseEntered(), mouseExited(), moustPressed(), and mouseReleased() to handle the mouse events. Interface MouseListener contains five methods: moustClicked(), mouseEntered(), mouseExited(), moustPressed(), and mouseReleased() to handle the mouse events.

Event Listener (cont.) NameMethod(s) ActionListeneractionPerformed() AdjustmentListeneradjustmentValueChanged() FocusListener focusGained(); focusLost() KeyListener keyPressed(); keyReleased(); keyTyped() MouseListener mouseClicked(); mouseEntered(); mouseExited(); moustPressed(); mouseReleased()

Implementing Interface To make use of the methods in an event listener interface in a class, it is implemented, instead of extended, by the class. To make use of the methods in an event listener interface in a class, it is implemented, instead of extended, by the class. If there are more than one interface to be brought in, they can be listed one after another with commas separating them. If there are more than one interface to be brought in, they can be listed one after another with commas separating them. The following code segment involves interfaces ActionListener and FocusListener in the EventDemo class. The following code segment involves interfaces ActionListener and FocusListener in the EventDemo class.

Implementing Interface (cont.) public class EventDemo extends JFrame implements ActionListener, FocusListener { implements ActionListener, FocusListener {... // code for EventDemo class... // code for EventDemo class}

Example: EventDemo.java The following is an interactive program that displays a message in a text field, whose background color can be changed by the user clicking one of the three buttons. The following is an interactive program that displays a message in a text field, whose background color can be changed by the user clicking one of the three buttons. In addition, the text style of the message is changed to italic when the field gains focus and returns to plain style when it loses focus. (The screen shot shows the window after the "cyan" button is clicked. Note that the text style is plain because the text field has lost its focus to the "cyan" button.) In addition, the text style of the message is changed to italic when the field gains focus and returns to plain style when it loses focus. (The screen shot shows the window after the "cyan" button is clicked. Note that the text style is plain because the text field has lost its focus to the "cyan" button.)

Example: EventDemo.java (cont.) Go to an Applet version of EventDemo

Example: EventDemo.java (cont.) Note Note In the class declaration, ActionListener and FocusListener are implemented. In the class declaration, ActionListener and FocusListener are implemented. Objects—buttons and textfield—are declared as instance variables (global to the class). Objects—buttons and textfield—are declared as instance variables (global to the class). In constructor EventDemo(), In constructor EventDemo(), buttons are associated with event listeners. E.g., green.addActionListener(this); buttons are associated with event listeners. E.g., green.addActionListener(this); Here, this refers the current class which will do the listening—like the “ear” Here, this refers the current class which will do the listening—like the “ear”

Example: EventDemo.java (cont.) Buttons are added to a container Buttons are added to a container Buttons must be associated with event listeners before the buttons are added to a container. Buttons must be associated with event listeners before the buttons are added to a container. Method actionPerformed() is event handler. Method actionPerformed() is event handler. Kind of object receiving the event is checked. Kind of object receiving the event is checked. Action is performed depending on the kind of object receiving the event. Action is performed depending on the kind of object receiving the event. Method main() simply instantiates the EventDemo class and makes the object visible. Method main() simply instantiates the EventDemo class and makes the object visible.

Example: DistanceConverter Program DistanceConverter attaches some calculation steps to event handlers. Its interface consists of two labels, two text fields, and four buttons. Program DistanceConverter attaches some calculation steps to event handlers. Its interface consists of two labels, two text fields, and four buttons. Go to an Applet version of EventDemo

Example: DistanceConverter (cont.) Text boxes Text boxes For inputting distances For inputting distances Buttons Buttons “To Meter”—when clicked, converts feet value to meter value and displays in appropriate box “To Meter”—when clicked, converts feet value to meter value and displays in appropriate box "To Feet"—when clicked, converts meters to feet value and displays in appropriate box. "To Feet"—when clicked, converts meters to feet value and displays in appropriate box. "Clear“—clears text fields "Clear“—clears text fields "Instructions“—pops up a message box with a short explanation of the program. "Instructions“—pops up a message box with a short explanation of the program.

Example: DistanceConverter (cont.) Here is the source code for DistanceConverter.java Here is the source code for DistanceConverter.javasource code for DistanceConverter.javasource code for DistanceConverter.java Note that: Note that: In constructor DistanceConvert() In constructor DistanceConvert() super() is an invocation to a constructor of the frame's super class. super() is an invocation to a constructor of the frame's super class. setdefaultCloseOperation() calls a method inherited from JFrame setdefaultCloseOperation() calls a method inherited from JFrame Container pane uses a grid layout of 4 rows by 2 columns Container pane uses a grid layout of 4 rows by 2 columns

Example: DistanceConverter (cont.) In event handler actionPerformed() In event handler actionPerformed() With interface ActionListener, this is the only method that needs to be implementd. With interface ActionListener, this is the only method that needs to be implementd. After the statement Object src = e.getSource() depending on the identity of src, different actions are performed. After the statement Object src = e.getSource() depending on the identity of src, different actions are performed. To check for valid input, note the use of strFeet.equals(""), and not strFeet == "", because strFeet is a reference, or a pointer. To check for valid input, note the use of strFeet.equals(""), and not strFeet == "", because strFeet is a reference, or a pointer.

Example: DistanceConverter (cont.) In the statement In the statement txtMeter.setText(decForm.format(numMeter)+" ") decForm is a formatting object which was instantiated from the DecimalFormat class, at the top of this method. The pattern in its constructor-- "#.00"--specifies two decimal points. Thus, expression decForm.format(numMeter) will return a string of digits with two decimal points.

Exercise Recall a simple java program created in the last chapter—GUI--named GUIDemo,java, which consisted of labels for first & last names, textboxes for first & last names, and buttons to start and quit the program. Recall a simple java program created in the last chapter—GUI--named GUIDemo,java, which consisted of labels for first & last names, textboxes for first & last names, and buttons to start and quit the program. Modify the program as follows: Modify the program as follows: Arrange the components using the grid layout manager. (3, 2) Arrange the components using the grid layout manager. (3, 2) Associate an event handler to the start button so that when it is clicked, a message box (showMessageDialog()) outputs a greeting. The greeting is of the form “Welcome to Hawaii, Charles Chang”, where “Charles” and “Chang” are contents of the two text boxes. Associate an event handler to the start button so that when it is clicked, a message box (showMessageDialog()) outputs a greeting. The greeting is of the form “Welcome to Hawaii, Charles Chang”, where “Charles” and “Chang” are contents of the two text boxes.

Exercise (cont.) Associate an event with the quit button so that when it is clicked, the program ends. (System.exit(0)) Associate an event with the quit button so that when it is clicked, the program ends. (System.exit(0))

Exercise (cont.) GuiDemo.java code