1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.

Slides:



Advertisements
Similar presentations
TCU CoSc Programming with Java Handling Events.
Advertisements

Event Handling.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
For IST410 Students only Events-1 Event Handling.
Event-Driven Programming
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Intermediate Java1 An example that uses inner classes Week Four Continued.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
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.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Welcome to CIS 083 ! Events CIS 068.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
 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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Introduction to Java Programming
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
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.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling CS 21a: Introduction to Computing I First Semester,
1 Lecture 8: User Interface Components with Swing.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Java Applets.
Lecture 8 Object Oriented Programming Using Java
Chapter 12 Event-Driven Programming
INFSY 547: WEB-Based Technologies
GUI III IS
Programming in Java Event Handling
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Java Applets.
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Final project.
Presentation transcript:

1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

What is an Event in Java? Defn: - in programming, an event is (in most cases) an action taken by the user. Types of events: 1)the user presses a key or clicks the mouse button. 2)the user clicks on a button, a choice, a checkbox, etc. Thus, rather than a program be executed as a long sequence of instructions, like the following: We have sections of the program which can be activated by the occurrence of an event. Sequential Execution

Basic Event Handling The GUI is responsible for constructing the user interface and for connecting (registering) widgets to listeners The listener part implements the appropriate interface for the type of event(s) of interest. The code (in the action handler) that performs the program’s action associated with the event(s).

The Event Loop In Java, the programmer creates a section(s) of code (methods) which will be automatically invoked whenever an event happens. Details of the event (the name of the button that was pressed, the choice box that was changed, the radio button that was pressed, etc) are returned to the programmer so that the required processing takes place. In Java, events are classified into several classes (we will study the following): 1)ActionEvents - generated whenever a change is made to a JButton, JCheckBox, JComboBox, JTextField, or JRadioButton.. 2)MouseEvents - generated whenever mouse moves, being clicked and etc... 3)ItemEvents-generated whenever a change is made to a JButton, JCheckBox, JComboBox, or JRadioButton.

Event Handlers that We Will Study Listener interfaces and related classes Event ClassListener Interface MethodsAssociation MethodsGenerated by Class ActionEventinterface ActionListeneraddActionListenerJButton actionPerformed(ActionEvent)removeActionListenerJCheckBox JComboBox JTextField JRadioButton ItemEventinterface ItemListeneraddItemListenerJCheckBox itemStateChanged(ItemEvent)removeItemListenerJRadioButton JComboBox MouseEventinterface MouseListeneraddMouseListenergenerated by mouse mousePressed(MouseEvent)removeMouseListenerevent on any component mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent)

Using Mouse Events 1.Import the required event classes; thus giving your applet the power to receive and handle messages, such as those sent when a button is clicked or the mouse is moved. import java.awt.event.*; 2.State that the class implements an MouseListener. public class EventHandlingPractice extends Applet implements MouseListener 3.Inform the applet that this object will respond to mouse events, using addMouseListener. addMouseListener(this); 4.Provide a method called mouseClicked() to be invoked when a mouse event occurs. public void mouseClicked( MouseEvent e )

What are the Parts? import java.awt.*; import java.awt.event.*; public class IRS extends Applet implements MouseListener { public void init() { addMouseListener(this); } public void mouseClicked( MouseEvent e ) { } Step #1 Step #2 Steps #3 Step #4

import java.awt.*; import java.awt.event.*; public class MouseEventDemo extends Applet implements MouseListener { String message; public void init() { message = “”; addMouseListener(this); } public void mousePressed(MouseEvent e) { message += "Mouse pressed”; } public void mouseReleased(MouseEvent e) { message = "Mouse released”; } public void mouseEntered(MouseEvent e) { message = "Mouse entered”; } public void mouseExited(MouseEvent e) { message = "Mouse exited”; } public void mouseClicked(MouseEvent e) { message = "Mouse clicked”; } MouseEvents

Mouse events are generated by the following types of user interaction: –A mouse click –A mouse entering a component's area –A mouse leaving a component's area Any component can generate these events, and a class must implement the MouseListener interface to support them. Methods: void mouseClicked(MouseEvent e) – invoked when the mouse button has been clicked (pressed and released) on a component. void mouseDragged(MouseEvent e) – invoked when a mouse button is pressed on a component and then dragged. void mouseEntered(MouseEvent e) - Invoked when the mouse enters a component. void mouseExited(MouseEvent e) - Invoked when the mouse exits a component. void mouseMoved(MouseEvent e) - Invoked when the mouse cursor has been moved onto a component. void mousePressed(MouseEvent e) - Invoked when a mouse button has been pressed on a component. void mouseReleased(MouseEvent e) - Invoked when a mouse button has been released on a component. MouseEvents