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.

Slides:



Advertisements
Similar presentations
Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
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.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
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-Driven Programming Event:  A signal to the program that something has happened.  It can be triggered either by external user actions, such as mouse.
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.
GUI Programming in Java: Event Handling & More Components Corresponds with Chapter 14, Chapter 15.
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.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphical User Interface Components: Part 1 Chapter 11.
– 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.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
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.
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
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.
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.
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.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
Creating User Interfaces Event-Driven Programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
Sep 181 Example Program DemoTranslateEnglishGUI.java.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
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 using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
Handling User Events with Swing
Advanced User Interfaces
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
Event Handling CS 21a: Introduction to Computing I
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Event-driven programming for GUI
Making Java GUIs Functional
Presentation transcript:

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 with the GUI

Some typical component events and listeners Act that results in eventListener User clicks a button, presses return while typing in a text field, or chooses a menu item ActionListener User closes a window WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener

Implementing listeners (1) Three key bits of code 1) add interface 2) register 3) handle Components can have multiple listeners A simple JButton ActionListener…

Implementing listeners (2) public class myClass … implements ActionListener { … // where setting up occurs (e.g. constructor) JButton button = new JButton(I am a button); button.addActionListener(this); … public void actionPerformed(ActionEvent e) { …// respond to event } // end response method } // end class

Types of event listeners (1) Global component listeners may be used for any Swing components Types ComponentListener (changes in size, position, visibility) FocusListener (whether ability for keyboard input) KeyListener (key press events, only with focus) MouseListener (clicks and movement into/out of component area) MouseMotionListener (changes in position over component)

Types of event listeners (2) Component-specific listeners relevant to specific components actions Types ActionListener CaretListener ChangeListener DocumentListener ItemListener ListSelectionListener WindowListener etc. See: s.html s.html

Getting event information EventObject class - use sub classes of this to determine whats happened. Get the firing object with getSource(); Actual event classes sometimes have specific types e.g. the ComponentListener uses a sub-class of EventObject : ComponentEvent that has getComponent(); Event classes may define methods that return more information e.g. ActionEvent has a method for getting modifiers (Shift, Alt, Ctrl)

Low-level and semantic events (1) Low-level events - window-system level e.g. mouse, key, component, container, focus, window trigger component-independent Semantic events everything else! – e.g. action, item, list selection trigger can differ by component e.g. button click and textfield return action events

Adapters for event handling (1) Classes which implement listener interfaces must implement all listener methods e.g. MouseListener has 5 methods: mouseClicked, mouseReleased, mousePressed, mouseEntered, mouseExited This leads to cluttered code Say you only want mouseClicked to do something then all others have to be implemented but empty Alternative….