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.

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 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Event Handling.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
For IST410 Students only Events-1 Event Handling.
Event-Driven Programming
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
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.
 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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
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.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
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,
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.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
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.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
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
Handling User Events with Swing
Event Handling Chapter 2 Objectives
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
Event-driven programming for GUI
Unit I: Chapter 1 Event handling.
AWT.
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Making Java GUIs Functional
Presentation transcript:

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. n Event Listeners: A listener is an object that is notified when an event occurs. ä It must have been registered with one or more sources to receive notification about specific types of events, ä It must implement methods to receive and process these notification.

The Event delegation Model n Event classes n Event listeners n Explicit event enabling n Adapters

The Event Classes n GUIs are event-driven, which generate events when the user of the program interacts with the GUI. n When a user interaction occurs an event is automatically sent to the program. n GUI information is stored in an object of a class that extends AWTEvent.

Event Class Hierarchy java.util.EventObject java.awt.AwtEvent ActionEventAdjustmentEvent ComponentEventItemEvent TextEvent ContainerEventFocusEventInputEventPaintEventWindowEvent KeyEventMouseEvent

The Event Classes n java.util.EventObject ä Object getSource(): returns the object that originated the event. n java.awt.AWTEvent ä int getID(): returns the ID of the event

The Event Classes n ActionEvent: generated by activation of components. n AdjustmentEvent: generated by adjustment of adjustable components such as scroll bars n ContainerEvent: generated when components are added or removed from a container. n FocusEvent: generated when a component receives or loses input focus.

The Event Classes n ItemEvent: generated when an is selected from a list, choice, or check box. n KeyEvent: generated by keybord activity. n MouseEvent: generated by mouse activity. n PaintEvent: generated when a component is painted. n TextEvent: generated when a text component is modified. n WindowEvent: generated by window activity (such as iconifying or de-iconifying.)

The Event Classes n InputEvent: ä long getWhen(): returns the time when the event took place. n MouseEvent: ä int getX(), getY(): returns the position of the mouse within the originating component at the time the event took place.

Event Listeners n An event listener is an object to which a component has delegated the task of handling a particular kind of event. n When the component experiences input, an event of the appropriate type is constructed, the event is then passed as the parameter to a method call on the listener. n A listener must implement the interface that contains the event-handling method.

java.awt.event n ActionListener n AdjustmentListener n ComponentListener n ContainerListener n FocusListener n ItemListener

java.awt.event n KeyListener n MouseListener n MouseMotionListener n TextListener n WindowListener

Event Handling n Register an event listener: Button button = new Button(“1”); ButtonHandler bHandler = new ButtonHandler(); button.addActionListener(bHandler); n Implement an event Handler: private class ButtonHandler implements ActionListener { public void actionPerformed (ActionEvent e ) { if (e.getActionCommand()== "+/-") { do something…. }

Adapters n An adapter is simply a class that implements an interface by providing do-nothing methods. public static man(String[] args) { Frame aFrame = new Frame(); Frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit( 0 ); }

WindowListener Frame aFrame = new Frame(); Frame.addWindowListener( new MyWindowListener() ); …… } class MyWindowListener implements WindowListener {

MyWindowListener { windowActivated(WindowEvent) { } windowClosed(WindowEvent) { } windowClosing (WindowEvent) {System.exit(0);} windowDeactivated (WindowEvent){ } windowDeiconified (WindowEvent){ } windowIconfied (WindowEvent){ } windowOpened (WindowEvent){ }

WindowListener n windowActivated(WindowEvent) n windowClosed(WindowEvent) n windowClosing (WindowEvent) n windowDeactivated (WindowEvent) n windowDeiconified (WindowEvent) n windowIconfied (WindowEvent) n windowOpened (WindowEvent)

Explict Event Enabling 1.Create a subclass of the component. 2. In the subclass constructor, call enableEvents(AWTEvent.XXX_Event_Mas k). 3. Provide the subclass with a processXXXEvent() method; this method should call the superclass’ version before returning.

Explict Event Enabling class MyBtn extends Button { public MyBtn(String label) { super(label); enableEvents(AWTEvent.ACTION_EVENT_MASK); } public void processActionEvent(ActionEvent ae) { System.out.println(“processing an action event”); super.processActionEvent(ae); }