Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,

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.
Introduction to Java 2 Programming
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
Event-Driven Programming
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.
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.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
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.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
1 CSE 331 More Events (Mouse, Keyboard, Window, Focus, Change, Document...) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Swing II. Swing II Topics WindowListener interface WindowListener interface Icons Icons Scrollbars Scrollbars The Graphics class The Graphics class Colors.
 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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
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.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
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,
Creating User Interfaces Event-Driven Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Event Handling and Listeners in SWING The practice of event handling.
CS 240 Week 9. EventQueue.invokeLater In Swing, all user interface operations must occur on the “UI thread” – All components should be created on the.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Events and Event Handling
Welcome To java
CHAPTER Reacting to the user.
Event Handling Chapter 2 Objectives
Advanced User Interfaces
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Event Handling Nithya Raman.
Introduction to Computing Using Java
Swing II.
GUI Programming using Java - Mouse Events
Events, Event Handlers, and Threads
Presentation transcript:

Event Handling

User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events, Component changes – High-level component-specific events Button events, Menu events, List events, etc. Events are handled by attaching “event listeners” to windows and components Swing defines a “listener interface” for each category of events – Examples: WindowListener, MouseListener, MenuListener

Event Handling A “listener” is an object that implements a listener interface Listeners are attached to windows and components by calling the appropriate “addXXXListener” methods – addWindowListener, addMouseListener, addMenuListener, etc. The window or component will notify all attached listeners about all relevant events – windowActivated, mouseClicked, menuSelected, etc. Listener methods accept an EventObject parameter that describes the event that occurred – WindowEvent, MouseEvent, MenuEvent, etc. – The EventObject.getSource method returns the window or component that generated the event

Event Handling Listeners are typically implemented as inner classes – Named inner class class MyMouseHandler implements MouseListener { … } this.addMouseListener(new MyMouseHandler()); – Anonymous inner class this.addMouseListener(new MouseListener() { … });

Event Handling Event Adapter classes – Frequently, a program only wants to handle a few of the events defined by a listener interface – However, implementing the listener interface requires implementations for all event methods – Swing provides Adapter superclasses that provide empty implementations of all methods in a listener interface WindowAdapter, MouseAdapter, KeyAdapter, etc. – This allows a program to create a listener by subclassing an adapter class, and overriding only the event methods it cares about – Some adapter classes implement multiple listener interfaces EXAMPLE: MouseAdapter implements MouseListener, MouseMotionListener, and MouseWheelListener

Mouse Events MouseListener interface MouseMotionListener interface MouseWheelListener interface MouseAdapter class EXAMPLE: Dragging and scrolling shapes in DrawingDrawing

Component Events ComponentListener interface ComponentAdapter class EXAMPLE: Updating width and height in text shapes in DrawingDrawing

Window Events WindowListener interface WindowStateListener interface WindowAdapter class EXAMPLE: Frame window in DrawingDrawing

Keyboard Events Keyboard Focus – When the user types a key, which component gets the key event? The component with the “keyboard focus” – Only one window has the keyboard focus at a time – Within that window, only one component has the keyboard focus at a time – A component can request the keyboard focus by calling Component.requestFocus or Component.requestFocusInWindow – EXAMPLE: A text field requests the keyboard focus when the user clicks on it so it will receive key events

Keyboard Events When a window receives or loses the keyboard focus, it generates “gained focus” and “lost focus” events – WindowFocusListener interface When a component receives or loses the keyboard focus, it generates “gained focus” and “lost focus” events – FocusListener interface EXAMPLE: In Drawing, when the frame window receives keyboard focus, DrawingFrame calls requestFocusInWindow on the DrawingComponent so it will receive keyboard eventsDrawing

Keyboard Events KeyListener interface KeyAdapter class EXAMPLE: Moving shapes with arrow keys in DrawingDrawing