OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -

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

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
TCU CoSc Programming with Java Handling Events.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
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.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
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.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
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,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
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.
– 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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
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.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
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-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
Chapter 6: Events in Java 1.0 Model –Event class –selecting component to handle –handleEvent method –helper methods (action, keyUp, mouseUp, etc.) 1.1.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
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.
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.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Applets.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Handling User Events with Swing
Event Handling Chapter 2 Objectives
Programming in Java Event Handling
GUI Programming III: Events
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Presentation transcript:

OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -

OOP&M - theory lectures2 OOP&M – Event Handling Event Model The Java event model is used by both the AWT and Java Beans (Java Beans will be explained during the 2nd course) Every event is a subclass of java.util.EventObject AWT events, the ones under study here, have been placed in the java.awt.event package (so it is easier to use them) Every event has a source object, which can be obtained with the method: getSource(), and every event has a type value, which can be obtained with getID() The type value is used to distinguish the various types of events that are represented by the same event class. E.g. The event MouseEvent can distinguish different events including the one called MouseEvent.MOUSE_PRESSED or the MouseEvent.MOUSE_DRAGGED between others

OOP&M - theory lectures3 OOP&M – Event Handling Event Model II Event classes contain data values that correspond to the particular event type. E.g. MouseEvent has getX(), getY() and getClickCount() methods The Event Handling model is based on the concept of an “event listener”. An object interested in receiving events is an event listener An object that generates events (event source) maintains a list of listeners that are interested in being notified when an event occurs, and provides methods that allow listeners to add themselves and remove themselves from this list of interested objects When the event source generates an event, the event source notifies all the listener objects that the event has occurred

OOP&M - theory lectures4 OOP&M – Event Handling Event Model III An event source notifies an event listener object by invoking a method on it and passing it an event object (an instance of a subclass of EventObject ) For this reason, we must implement methods with PREDEFINED names in order to treat the different events E.g. If we want the program to execute an action when we click with the mouse, the event is the MouseEvent, its listener is the MouseListener and the method to implement is called: mouseClicked() One event defines more than one method, as far as the objects affected by that event can interact in different ways. E.g. The event KeyEvent, that controls the keyboard, can be assigned to the methods: keyPressed(), keyReleased() and keyTyped()

OOP&M - theory lectures5 OOP&M – Event Handling Event Model IV The following slides show the relationship between events, listeners and methods Event classListener InterfaceListener methods ActionEventActionListeneractionPerformed() AdjustmentEventAdjustmentListeneradjustmentValueChanged() ComponentEventComponentListenercomponentHidden() componentMoved() componentResized() componentShown() ContainerEventContainerListenercomponentAdded() componentRemoved() FocusEventFocusListenerfocusGained() focusLost() ItemEventItemListeneritemStateChanged() KeyEventKeyListenerkeyPressed() keyReleased() keyTyped()

OOP&M - theory lectures6 OOP&M – Event Handling Event Model V Event classListener InterfaceListener methods MouseEventMouseListenermouseClicked() mouseEntered() mouseExited() mousePressed() mouseReleased() MouseMotionListenermouseDragged() mouseMoved() TextEventTextListenertextValueChanged() WindowEventWindowListenerwindowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() if you implement the listener to one event, you have to declare ALL the methods, so if you are interested in only one of them, you have to declare the heads of the rest.

OOP&M - theory lectures7 OOP&M – After the party

OOP&M - theory lectures8 OOP&M – After the party BjornLisa Events of the classes put and order Listener: He handles the events: -order the records -put the lyrics into the folders -put the folders in their place But not: -clean the floor -bring the food to the kitchen Event Source: She produces the events: -order the records -put the lyrics into the folders -put the folders in their place -clean the floor -bring the food to the kitchen

OOP&M - theory lectures9 Bjorn Listener: Needs to be declared before, therefore Lisa must warn him than he will have to collaborate after the party class Bjorn implements Aspirin { … } OOP&M – After the party The different actions must be declared and the things that he is not going to do, too void orderDisc(LisaEvent e){ … } void putLyrics(LiseEvent e){ … } void cleanFloor(LisaEvent e){;}

OOP&M - theory lectures10 OOP&M – Event Handling Event Model VI For each of the event listener interfaces that contains more than one method, java.awt.event defines a simple “adapter” class that provides an empty body for each of the methods in the interface So you have to override each one of those methods when you want to control one event Now we are going to compare different cases with different events, but first we need to make a different approach to the problem We work with elements of the GUI, so the question now is which events affect to those objects (buttons, menus, scrollbars, windows, etc.)

OOP&M - theory lectures11 OOP&M – Event Handling Event Model VII Question: Which events are related to our GUI elements? ComponentEvents GeneratedMeaning ButtonActionEventUser clicked on the button CheckboxItemEventUser deselected or selected an item CheckboxMenuItemItemEventUser deselected or selected an item ChoiceItemEventUser deselected or selected an item ComponentComponentEventComponent moved, resized, hidden or shown FocusEventComponent gained or lost focus KeyEventUser pressed or released a key

OOP&M - theory lectures12 OOP&M – Event Handling Event Model VIII ComponentEvents GeneratedMeaning ComponentMouseEventUser pressed or released mouse button, mouse entered or exited a component or user moved a or dragged the mouse ContainerContainerEventComponent added to or removed from a container ListActionEventUser double-clicked item ItemEventUser deselected or selected an item MenuItemActionEventUser selected a menu item ScrollbarAdjustmentEventUser moved the scrollbar TextComponentTextEventUser changed text TextFieldActionEventUser finished editing txt WindowWindowEventWindow opened, closed, iconified, deiconified or close request

OOP&M - theory lectures13 OOP&M – Event Handling – Working method Event Model IX What shall be the question when we want to control an event? Are we looking for controlling something that happens (mouse movements, clicks, key pressed...) Are we looking for controlling a GUI’s component? Use 1st table and look for the special event that you want to control Use 2nd table and look for exactly the component that you want to control A B

OOP&M - theory lectures14 OOP&M – Event Handling – generalizing import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicKey extends Applet implements XListener { public void init() { r = new Object(...); add(r); r.addXListener(this); } public void XaOccurred(XEvent event) { s = getInformationFromX(); DoSomethingWithTheInformation(); } public void XbOccurred(KeyEvent event) {;} private Object r; }

OOP&M - theory lectures15 OOP&M – Event Handling – the keyboard Event Model X Imagine a case of the first class, imagine that we want to control the keyboard, it is quite clear from table one that we must look for a KeyEvent Example: KeyEventKeyListenerkeyPressed() keyReleased() keyTyped() From the table we can extract the following information: –KeyEvents require the implementation of KeyListeners –Each KeyListener includes three different methods: if a key was pressed if it was released if it was typed (pressed and released)

OOP&M - theory lectures16 OOP&M – Event Handling – the keyboard Exercise Knowing that the keyboard is a source of KeyEvents, and that the KeyListener can attend to the methods: keyPressed(), keyReleased() and keyTyped(), try to write a program that shows a Label component where it will be shown which key was typed [Hint: *.getKeyText() *.getKeyCode() are needed for detecting which key was typed ]

OOP&M - theory lectures17 OOP&M – Event Handling – the keyboard import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicKey extends Applet implements KeyListener { public void init() { result = new Label(" "); add(result); result.addKeyListener(this); } public void keyPressed(KeyEvent event) { String s = event.getKeyText(event.getKeyCode()); result.setText(s); doLayout(); } public void keyTyped(KeyEvent event) {;} public void keyReleased(KeyEvent event) {;} private Label result; }

OOP&M - theory lectures18 OOP&M – Event Handling – the keyboard import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicKey extends Applet implements KeyListener { public void init() { result = new Label(" "); add(result); result.addKeyListener(this); } public void keyPressed(KeyEvent event) { String s = event.getKeyText(event.getKeyCode()); result.setText(s); doLayout(); } public void keyTyped(KeyEvent event) {;} public void keyReleased(KeyEvent event) {;} private Label result; } Declaration of the listener of the events on the keyboard Here we override the declaration of the method that will be called by the interface Even if we don’t use them, we must declare the rest of the methods for this listener

OOP&M - theory lectures19 OOP&M – Event Handling – Working method Event Model XI What shall be the question when we want to control an event? Are we looking for controlling something that happens (mouse movements, clicks, key pressed...) Are we looking for controlling a GUI’s component? Use 1st table and look for the special event that you want to control Use 2nd table and look for exactly the component that you want to control A B

OOP&M - theory lectures20 OOP&M – Event Handling – a Checkbox Event Model XII Imagine a case of the second class, imagine that we want to control a Checkbox, from table two we get the information about which events are related to the object. In this case is ItemEvent Example: ItemEventItemListeneritemStateChanged() From the table we can extract the following information: –Checkboxes are controlled by ItemEvents –ItemEvents require the implementation of ItemListeners –Each ItemListener includes only one possible method: if the state of the item changed

OOP&M - theory lectures21 OOP&M – Event Handling – the keyboard Exercise Knowing that a CheckBox is a source of ItemEvents, and that the ItemListener can attend to the methods: ItemStatechanged(), try to write a program that shows a Checkbox component where it will be shown that is is selected on its Label [Hint: *.setLabel() is needed for showing the text, *.getState() for knowing the state ]

OOP&M - theory lectures22 import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicCheckbox extends Applet implements ItemListener { public void init() { mybox = new Checkbox("not checked"); add(mybox); mybox.addItemListener(this); } public void itemStateChanged(ItemEvent event) { if (mybox.getState()) { mybox.setLabel("checked"); } else { mybox.setLabel("not checked"); } doLayout(); } private Checkbox mybox; } OOP&M – Event Handling – a Checkbox

OOP&M - theory lectures23 import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicCheckbox extends Applet implements ItemListener { public void init() { mybox = new Checkbox("not checked"); add(mybox); mybox.addItemListener(this); } public void itemStateChanged(ItemEvent event) { if (mybox.getState()) { mybox.setLabel("checked"); } else { mybox.setLabel("not checked"); } doLayout(); } private Checkbox mybox; } Declaration of the listener of the item’s events Here we override the declaration of the method that will be called by the interface There are not other methods for this listener OOP&M – Event Handling – a Checkbox

OOP&M - theory lectures24 OOP&M – Event Handling – Inner Classes import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble3 extends Applet implements MouseListener, MouseMotionListener { public void init() { b = new Button("Toggle Color"); add(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (status) { result.setText("Red"); status = false; } else { result.setText("Black"); status = true; } doLayout(); //b.removeActionListener(this); } }); result = new Label("Black"); add(result); } private Button b; private Label result; private boolean status = true; }