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.

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

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.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
For IST410 Students only Events-1 Event Handling.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
Event-Driven Programming
Event-Driven Programming
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.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
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.
Io package as Java’s basic I/O system continue’d.
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
(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.
– 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.
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.
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.
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.
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.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Events and Event Handling
Chapter 14 Event-Driven Programming
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.
Event Handling Chapter 2 Objectives
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Presentation transcript:

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 model. l Example 1: Handling key events. l Example 2: Handling mouse events. l Adapter classes. l Coming Next: GUI Programming (Part II).

2 Gui Programming (Part I) Introduction to Events  GUI applications are event-driven. User interaction with the GUI results in events being generated to inform the application of user actions. Clicking a button, closing a window, or hitting a key results in an appropriate event being sent to the application  Events can be generated as a consequence of a person interacting with graphics components of a program. Examples:  Keyboard events (key up, key down)  Mouse events (mouse move, mouse drag, button up, button down).  Button events (user presses a button)  TextField events (user presses a return)  Menu events (user selects an item from a pull-down menu)  Events can also happen in cases where there is no direct user interaction. For example, an event may be generated when a timer expires, a counter exceeds a value, a software or hardware failure occurs, or an application is completed.

3 Gui Programming (Part I) Introduction to Events (Cont ’ d)  When studying events and events handling, there are three main categories of classes to deal with:  Event classes.  Event source classes.  Event listener classes.  Event classes represent events and provide a consistent, easy-to-use means of encapsulating events.  Event sources are graphics components (like Buttons, Scollbar, Window) which generate and manage event listeners. For example, you receive key and mouse events from an applet (see Examples 1 and 2 in this lecture)  Listener classes are classes that implement one or more listener interfaces defined by the java.awt.event package. Event class (e.g. MouseEvent ). Contains information about the event (e.g. mouse location). Event listener. Knows what to do when the event occurs. Implements listener interface (e.g. MouseListener ). Event source. Keeps a list of listeners and notifies them when the event occurs.

4 Gui Programming (Part I) A Brief History l The original AWT (Abstract Windowing Toolkit)was suitable for Java applets but not for full-fledged application development. l AWT 1.1 (JDK 1.1) had better event handling but did not have enough GUI components and was too dependent on (nonportable) native code,. In 1997 Netscape and Sun developed a set of GUI classes written entirely in Java. The Java Foundation Classes (JFC), including the Swing component set, were released with JDK 2.0. l A Swing program can have the same look and feel on a Mac, Windows, or Unix platform.

5 Gui Programming (Part I) Event Classes & Source classes

6 Gui Programming (Part I) Java Events Classes  At the root of the Java event class hierarchy is EventObject, which is in java.util (also see Horstmann Chapter 10).  The EventObject contains two methods: getSource() and toString() which return the source event and the string equivalent of the event respectively.  Event classes contain methods for getting information on the event, for example the MouseEvent class has methods getX() and getY() returning the coordinates of the mouse when the event occurs.  Here are some of the event classes which subclass EventObject:  ActionEvent : A component-defined action occurred.  ComponentEvent : A component moved, changed size, or changed visibility.  FocusEvent : A component has gained or lost the keyboard focus.  KeyEvent : A keystroke occurred in a Component.  MouseEvent : A mouse action occurred in a Component.  TextEvent : An object’s text changed.  WindowEvent : A window has changed its status.

7 Gui Programming (Part I) Event Sources and Listeners  An object that generates an event is called a source. A source may generate more than one event.  When an event occurs (or is "fired"), it is received by one or more "listeners" which act on the event.  Note that an event source and the place where the event is handled can be separate.  Each event listener is an object of a class that implements a particular type of listener interface.  A listener class must implement the appropriate interface and all the event handling logic will go inside the listener class.  The most commonly handled events are those generated by the mouse, the keyboard, and various controls, such as a push button.

8 Gui Programming (Part I) Event Sources and Listeners (Cont’d)  A source must register listeners in order for the listeners to receive notifications about a specific event.  The source object maintain la list of listener. example. keyboard event implements KeyListener interface Registration methods: are dependent on event type. Example KeyListener is addKeyListener. ActionEvent is addActionEvent  Each component type has the following pair of methods for registering and unregistering listeners  public void addTypeListener (TypeEvent t) throws java.util.TooManyListnersException  public void removeTypeListener (TypeEvent t)  Here, Type is the name of the event and t is a reference to the event listener. the TooManyListenersException is thrown in case of sources that allow only one listener to register.  Notifying many registered listeners for an event is called broadcasting the event. Event notification in sources that can have only one listener sources is called unicasting.

9 Gui Programming (Part I) The Delegation Model Events are sent from single source object Listener implements specific event handling method 1 Define a class that implements the appropriate listener interface your class provides the handler by implementing the methods that that are declared by the interface. Ex public class K implements KeyListener…MouseLisener…WindowListener. Implement  keyPressed,keyReleased, keyTyped 2-Register an instance of the class with the component affected by the event Ex. void addxxxListener(xxListener object). mouseEvent keyEvent windowEvent

10 Gui Programming (Part I) Example 1: Handling Key Events  In order to handle keyboard events, we must implement the KeyListener interface: void keyPressed(KeyEvent ke); void keyReleased(KeyEvent ke); void keyTyped(KeyEvent ke);  Note that when a key is pressed a KEY_PRESSED event is generated which results in calling the keyPressed() event handler.  When the key is released a KEY_RELEASED event is generated which results in calling the keyReleased() event handler.  If a character is generated by the keystroke, then a KEY_TYPED event is sent and the keyTyped() handler is invoked.  Thus, you will notice that, each time the user presses a key, at least two and often three events are generated. If you are interested in all these events then you must put the appropriate code in all these methods to achieve the behavior you desire.

11 Gui Programming (Part I) Example 1: Handling Key Events (Cont’d) import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class KeyEventHandler extends Applet implements KeyListener{ private String msg = ""; private int startX = 10, startY = 10; public void keyPressed(KeyEvent ke){ showStatus("Key Down"); } public void keyReleased(KeyEvent ke){ showStatus("Key Up"); } public void keyTyped(KeyEvent ke){ msg += ke.getKeyChar(); repaint(); } public void init(){ // pass the ref this to add itself as // a key listener addKeyListener(this); //The following method requests that //this component gets the input focus. //The component must be visible on the //screen for this request to be granted requestFocus(); } public void paint(Graphics g){ g.drawString(msg,startX,startY); }

12 Gui Programming (Part I) Example 2: Handling Mouse Events  In order to handle mouse events, we must implement the MouseListener interface which has these five methods : void mouseClicked(MouseEvent me); void mouseEntered(MouseEvent me); void mouseExited(MouseEvent me); void mousePressed(MouseEvent me); void mouseReleased(MouseEvent me); If mouse is clicked and released at the same point, mouseClicked() is invoked. When the mouse enters a component, the mouseEntered() method is called. When it leaves, mouseExited() is called.  The mousePressed() and mouseReleased() are invoked when the mouse is pressed and released respectively

13 Gui Programming (Part I) Example 2: Handling Mouse Events (Cont’d) import java.applet.Applet; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class MouseEventHandler extends Applet implements MouseListener{ public void mouseClicked(MouseEvent me) { System.out.println("Clicked at ("+me.getX()+", "+me.getY()+")"); } public void mouseEntered(MouseEvent me) { System.out.println("Entered at ("+me.getX()+", "+me.getY()+")"); } public void mouseExited(MouseEvent me) { System.out.println("Exited at ("+me.getX()+", "+me.getY()+")"); } public void mousePressed(MouseEvent me) { System.out.println("Pressed at ("+me.getX()+", "+me.getY()+")"); } public void mouseReleased(MouseEvent me) { System.out.println("Released at ("+me.getX()+", "+me.getY()+")"); } public void init() { addMouseListener(this); } }

14 Gui Programming (Part I) Mouse Event Example

15 Gui Programming (Part I) Introduction to Adapter Classes In order to make the button work, we need a WindowEvent listener. The obvious way to get such a listener is to write a listener class that implements the WindowListener interface. That turns out to be a bad idea., though. Implementing this interface requires writing seven methods(windowActivated()windowclosed(), windowIconified,windowOpened.., one for each type of window event. We’re interested in just one of these events, so we’d end up writing six methods for events that we don’t need. The java.awt.event package contains a class named WindowAdapter. This class implements the WindowListener interface, although the methods that it provides are all empty. All we have to do is extend the WindowAdapter class and override the WindowClosing method, which is the only one we’re interested in. WindowAdapter is an example of an adapter class. A class that we can extend instead of implementing an interface. Java provides matching adapter classes for most interfaces that have two or more methods.

16 Gui Programming (Part I) Introduction to Adapter Classes (Cont’d)  When an interface contains only one method, there’s no need for an adapter class Adapter classes save programmer’s time by providing an empty implementation of Listener’s methods  To use an Adapter you need to subclass it and override only the methods which you wish to use.  You can define an Adapter class as an inner, anonymous inner or as an external class.  Some adapter classes are: » java.awt.event.ComponentAdapter » java.awt.event.FocusAdapter » java.awt.event.KeyAdapter »java.awt.event.MouseAdapter »java.awt.event.MouseMotionAdapter »java.awt.event.WindowAdapter