95-713 Intermediate Java1 An example that uses inner classes Week Four Continued.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
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.
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.
For IST410 Students only Events-1 Event Handling.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
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.
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications 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.
Java 212: Interfaces Intro to UML Diagrams. UML Class Diagram: class Rectangle +/- refers to visibility Color coding is not part of the UML diagram.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
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.
3461 Sequential vs. Event-driven Programming Reacting to the user.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
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.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
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.
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.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
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.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
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.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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.
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0.
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.
Event Handling and Listeners in SWING The practice of event handling.
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.
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.
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.
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.
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
GUI III IS
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Programming Graphical User Interface (GUI)
Presentation transcript:

Intermediate Java1 An example that uses inner classes Week Four Continued

Intermediate Java2 Event Handling Used by the Abstract Window Toolkit (AWT) – for basic GUI programming Java 1.0 Used by Swing -- Better components than AWT Java 2 Used by JavaBeans -- reusable software components, like Visual Basic, that can be manipulated in a builder tool

Intermediate Java3 Babysitting A baby in the home needs attention. Many events surrounding the baby are not easily ignored. Events can be of different types.

Intermediate Java4 One possible source of events

Intermediate Java5 Babysitting Before responding to an event, a babysitter typically takes note of the source and the type of event.

Intermediate Java6 Babysitting The sitter needs to know both the event type and the event source. Event source Event type Handler

Intermediate Java7 Event Handling The window manager software may generate hundreds of different events. Examples include mouse clicks, mouse movements, key strokes, and timer ticks. A programmer is typically interested in a small subset of all of the possible events that may occur.

Intermediate Java8 Events and Event Objects Since events may be of different types but still exhibit some shared traits (inheritance) Java represents the event classes in a hierarchy. The root class is called java.util.EventObject. The only common feature shared by all events is a source object. So we find the following method in the EventObject class : public Object getSource();

Intermediate Java9 EventObject AWTEvent ActionEventComponentEvent InputEvent WindowEvent MouseEvent KeyEvent The Event Type is described by these Classes. Object String getActionCommand() int getX() char getKeyChar() boolean isAltDown()Window getWindow() public Object getSource();

Intermediate Java10 ComponentEvent - A low-level event which indicates that a component moved, changed size, or changed visibility -For notification only -The AWT will automatically handle component moves and resizes internally so that GUI layout works properly regardless of whether a program is receiving these events or not.

Intermediate Java11 ActionEvent -Indicates that a component-defined action occured -High-level event is generated by a component (such as a Button) when the component-specific action occurs (such as being pressed).

Intermediate Java12 Event handling usually involves three types of objects Objects are used to hold and report on information about the event. Objects are typically the source of events. Objects, called listeners, are used to handle events.

Intermediate Java13 A mouse object A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked The listener object has methods that are called for particular events Event Source Event type object has data and methods Listener

Intermediate Java14 A mouse object A mouse object An event object that describes, say, the x and y coordinate of where the mouse was clicked The listener object has methods that are called for particular events Implements MouseListener The event object is sent to a listener method A mouse object must be told who its listener is.

Intermediate Java15 The Listeners There are different types of Listeners. MouseListeners WindowListeners ScrollBarListeners Etc.. These Listeners are interfaces. Remember what an interface provides? If class X implements an interface then class X promises to provide (at least) the methods declared in the interface.

Intermediate Java16 Some of the Listener Interface Hierarchy EventListener ActionListener abstract void actionPerformed(ActionEvent e); MouseListener abstract void mouseClicked(MouseEvent e) KeyListener abstarct public void keyTyped(KeyEvent e)KeyEvent A tagging interface with no methods.

Intermediate Java17 A Listener Interface public interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mouseReleased(MouseEvent e); } What does it mean if I claim that I implement this interface?

Intermediate Java18 An Example of creating a listener // MouseSpyApplet.java import java.applet.Applet; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; Applets can be an event source

Intermediate Java19 class MouseSpy implements MouseListener { public void mouseClicked(MouseEvent event) { System.out.println("Mouse clicked. x = " + event.getX() + " y = " + event.getY()); } public void mouseEntered(MouseEvent event) { System.out.println("Mouse entered. x = " + event.getX() + " y = " + event.getY()); } public void mouseExited(MouseEvent event) { System.out.println("Mouse exited. x = " + event.getX() + " y = " + event.getY()); } public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed. x = " + event.getX() + " y = " + event.getY()); } public void mouseReleased(MouseEvent event) { System.out.println("Mouse released. x = " + event.getX() + " y = " + event.getY()); } Since we are implementing the MouseListener interface we must provide all of these methods!! But, at least now we have a listener.

Intermediate Java20 public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } Create a listener. Tell the applet who to call with any MouseEvent objects.

Intermediate Java21 Spying on the mouse Java Applets need an HTML file.

Intermediate Java22 Creating a Listener Another approach Suppose a friendly sole created this class: public class MouseAdapter implements MouseListener { void mouseClicked(MouseEvent e){} void mouseEntered(MouseEvent e){} void mouseExited(MouseEvent e){} void mouseReleased(MouseEvent e){} } Now, suppose we extend this class. What must we provide? Note the empty bodies.

Intermediate Java23 …only those methods that we are interested in. The other methods, when called, do nothing. Java provides several classes called “Adapter” classes for this purpose. There are WindowAdapters, MouseMotionAdapters…

Intermediate Java24 Register the listener Suppose we have a Button object Button b = new Button(); We must determine what events a particular component generates. We can see from the documentation that the Button class has an addActionListener() method. We resister the listener through the addActionListener method. Button b ActionEvent Object ActionEvent Object addActionListener() ActionListene r

Intermediate Java25 Register The Listener We want to listen for the ActionEvent object coming from the button. So, we tell the button what object will listen for the ActionEvent object: b.addActionListener(sitter)

Intermediate Java26 Hit the X and the program exits Once again but with a window

Intermediate Java27 Create a WindowCloseSitter Class import javax.swing.*; import java.awt.event.*; public class CloseDemo { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); WindowCloseSitter h = new WindowCloseSitter(); f.addWindowListener(h); }

Intermediate Java28 class WindowCloseSitter implements WindowListener { public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } But we have to implement ALL of the functions

Intermediate Java29 Let’s use The WindowAdapter class public abstract class WindowAdapter implements WindowListener { public void windowClosing(WindowEvent e) {} public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } A built in class -- we already have the empty bodies!!

Intermediate Java30 The Window again import javax.swing.*; import java.awt.event.*; public class CloseDemo2 extends WindowAdapter { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); f.addWindowListener(new CloseDemo2()); } public void windowClosing(WindowEvent e) { System.exit(0); }

Intermediate Java31 Again with anonymous classes import javax.swing.*; import java.awt.event.*; public class CloseDemo3 { public static void main(String[] args) { JFrame f = new JFrame("Example"); f.setSize(400,100); f.setVisible(true); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } ); } }