Object Oriented Programming.  Interface  Event Handling.

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

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Event Handling.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
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.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
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.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
UID – Event Handling and Listeners Boriana Koleva
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
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.
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 DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
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.
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.
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.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Welcome To java
CompSci 230 S Programming Techniques
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Programming in Java Event Handling
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Chap 7. Building Java Graphical User Interfaces
Introduction to Computing Using Java
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Making Java GUIs Functional
Presentation transcript:

Object Oriented Programming

 Interface  Event Handling

 Using interface, we specify what a class must do, but not how it does this.  An interface is syntactically similar to a class, but it lacks instance variables and its methods are declared without any body.  An interface is defined with an interface keyword.

 Two types of access: ◦ public – interface may be used anywhere in a program ◦ default – interface may be used in the current package only  Interface methods have no bodies – they end with the semicolon after the parameter list. They are essentially abstract methods.  An interface may include variables, but they must be final, static and initialized with a constant value.  In a public interface, all members are implicitly public.

 A class implements an interface if it provides a complete set of methods defined by this interface. ◦ any number of classes may implement an interface ◦ one class may implement any number of interfaces  Each class is free to determine the details of its implementation.  Implementation relation is written with the implements keyword.

 Declaration of the Callback interface:  Client class implements the Callback interface:

 An event is an action initiated by the user interacting with the program.  Examples ◦ Keyboard events - pressing a key, holding a key, releasing a key ◦ Mouse events - moving the mouse, clicking the mouse ◦ GUI events - clicking on a button, resizing a window, closing a window, opening a window  An event in Java is an object of a particular event class, that represents some user actions to which the GUI might respond

 Low level events represent direct communication from the user  Low level event examples (all the event classes listed below belong to the java.awt.event package0: ◦ key event - a keyboard key pressed or released - in the KeyEvent class ◦ focus event – a component got focus, lost focus – in the FocusEvent class ◦ mouse event - the mouse is moved or dragged, a mouse button is pressed or released, the mouse cursor enters or exits a component - in the MouseEvent class ◦ component event - a component is hidden, shown, resized, or moved – in the ComponentEvent class ◦ container event - a component is added to or removed from a container in the ContainerEvent class ◦ window event - a window is opened, closed, activated, deactivated, etc. - in the WindowEvent class

 High level events usually involve one or more low level events  High Level Event examples ◦ action event - do a command – ActionEvent class ◦ adjustment event - represents scrollbar motions such as a value was adjusted – AdjustmentEvent class ◦ item event - occurs when the user selects a checkbox, choice, or list item, i.e. item state has changed –ItemEvent class ◦ text event – represents a text component content (value) change – TextEvent class

 When the user clicks the mouse on a button, then releases it, the button gets two or three separate, low level mouse events ◦ one for mouse down ◦ one for mouse up ◦ possibly one for mouse drag (if the user moves the mouse while the button is pressed)  However, the button then fires one high level event only - ActionEvent

 Events are organized into hierarchy of event classes  Event classes contain data relevant to a particular event type  An event is an object of one of the event classes

java.util.EventObject java.awt.AWTEvent AdjustmentEventComponentEventActionEventTextEventItemEvent WindowEventInputEventFocusEventContainerEvent MouseEventKeyEvent PaintEvent java.lang.Object

 The type of an event depends on its source  Example of event sources: ◦ the keyboard ◦ the mouse ◦ the GUI components – buttons, text fields, windows  Event source is an object with the ability to determine when an event has occurred

 In event driven programming the events “drive” the execution of the program, e.g. the code is executed when events are activated  The program interacts with the user and generates events based on the external user actions  Java Visual (Graphical) programming and Visual Basic programming are event driven  When writing applets that are using events in Java we have to import the “events” package java.awt.event.*;

 Java uses delegation-based model for event handling  Java uses event listener to register an event and event handler to respond to the event  The use of event listeners in event handling is called delegation event model

 An external user’s action on a source object (e.g. the event source) activates an event  An event listener object (e.g. an object interested in the event source) receives the event. This object is an instance of a class that implements a specific EventListener interface ◦ Example: ActionEvent --> ActionListener  The source maintains a list containing all the listener objects that have registered to be notified of events of that type

 The transmission of an event from an event source to an event listener involves invoking a method on the listener object by which the source notifies the listener of the occurrence of an event of a specific type ◦ Example: method actionPerformed (ActionEvent e) ◦ An EventListener interface declares one or more methods which must be defined in the listener class, and which are invoked by the event source in response to each specific event type handled by the interface  Example: EventListener  method actionPerformed (ActionEvent e)

 The Java standard class library contains several classes that represent typical events  Components, such as an applet or a button, generate (fire) an event when it occurs  Objects, called listeners, wait for events to occur. A listener object is an instance of a class that implements a specific listener interface  A number of listener interfaces are pre-defined and each interface declares the appropriate methods for a specific class of events

Component This object may generate an event Listener This object waits for and responds to an event Event When an event occurs, the component calls the appropriate method of the listener, passing an (event) object that describes the event

 Each event is represented by an object that gives information about the event and identifies the event source.  Each event source can have multiple listeners registered on it. A single listener can register with multiple event sources. event source event listener 1 event listener 2 event listener 3

 A listener object can be registered on a source object to be notified of the occurrence of all events of the specific class for which the listener object is designed  The occurrence of an event defined by the specified class will automatically invoke the matching method in the listener object  The code in the body of the method is designed by the programmer to perform the desired action when the event occurs

 We can create a listener object by writing a class that implements a particular listener interface  The Java standard class library contains several interfaces that correspond to particular event categories  After creating the listener, we add the listener to the component that might generate the event to set up a relationship between the component, generating the event and the event listener

 Register an event listener ◦ “listens” for events generated by GUI components ◦ an object of a class from the package java.awt.event  Implement an event handler ◦ a method that is automatically called in response to a particular type of event

 For each event class there is a corresponding listener interface defined in Java and corresponding listener methods (handlers) in the listener interface  Example : ◦ for the event class ActionEven t ◦ the listener is ActionListener ◦ and the listener method (handler) is actionPerformed (ActionEvent e )

 Commonly used AWT event Listeners ◦ ActionListener ◦ AdjustmentListener ◦ FocusListener ◦ ItemListener ◦ KeyListener ◦ MouseListener ◦ MouseMotionListener ◦ MouseWheelListener ◦ WindowListener ◦ WindowFocusListener ◦ WindowStateListener

 Every event handler requires three separate steps 1.In the declaration for the event handler class, we specify that the class either implements a listener interface or extends a class that implements a listener interface public class MyClass implements ActionListener { … } 2.Code that registers an instance of the event handler class as a listener upon one or more components someComponent.addActionListener(instanceOfMyClass); 3.Code that implements the methods in the listener interface public void actionPerformed(ActionEvent e) { //code that reacts to the action... }

 Adapter Class exists as convenience for creating a listener object.  Extend this class to create a listener for a particular listener interface and override the methods for the events of interest.  It defines null methods for all of the methods in the listener interface, so you can only have to define methods for events you care about.  Commonly used adapter classes: ◦ FocusAdapter ◦ KeyAdapter ◦ MouseAdapter ◦ MouseMotionAdapter ◦ WindowAdapter