Web Design & Development Lecture 13

Slides:



Advertisements
Similar presentations
Web Design & Development Lecture 19. Java Graphics 2.
Advertisements

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
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.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
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.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
PROG Object Oriented Programming II PROG Object Oriented Programming II Window Events Multi-screen Applications.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Swing II. Swing II Topics WindowListener interface WindowListener interface Icons Icons Scrollbars Scrollbars The Graphics class The Graphics class Colors.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Object Oriented Programming.  Interface  Event Handling.
©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.
Nested Classes CompSci 230 S Software Construction.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
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 Adapter Class. CSI 3125, Preliminaries, page 2 Adapter Class Java provides a special feature, called an adapter class,
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
CompSci 230 S Programming Techniques
CHAPTER Reacting to the user.
Web Design & Development Lecture 11
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
CompSci 230 S Programming Techniques
Java Programming: From Problem Analysis to Program Design,
A Quick Java Swing Tutorial
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Introduction to Computing Using Java
Inner Classes 29-Nov-18.
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Java Inner Classes.
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
...that can get messy when we have lots buttons!
Presentation transcript:

Web Design & Development Lecture 13

Event Handling part 3

Handling Window Events Example Revisited

Window Exit Handler Example Reveisted // Event Handlers for WindowListener public void windowActivated (WindowEvent we) { } public void windowClosed (WindowEvent we) { } public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } public void windowDeactivated (WindowEvent we) { } public void windowDeiconified (WindowEvent we) { } public void windowIconified (WindowEvent we) { } public void windowOpened (WindowEvent we) { }

Last Code Example Problem Solution Interested in windowClosing method only But have to provide definitions of all the methods, Why ? Because a class implementing an interface has to provide definitions of all methods present in that interface. Solution Use Adapter classes

Adapter Classes

Adapter Classes For listener interfaces containing more than one event handling methods, jdk defines adapter classes. Examples are For WindowListener  WindowAdapter For MouseMotionListener  MouseMotionAdapter and many more Adapter classes provides definitions for all the methods (empty bodies) of their corresponding Listener interface It means that WindowAdapter class implements WindowListener interface.

Adapter Classes public interface MouseMotionListener { public void mouseDragged (MouseEvent me); public void mouseMoved (MouseEvent me); } public class MouseMotionAdapter impelments MouseMotionListener { public void mouseDragged (MouseEvent me) { } public void mouseMoved (MouseEvent me) { } }

How to use Adapter Classes Previously handler class need to implement interface public class EventsEx implements MouseMotionListener { …. } Therefore it has to provide definitions of all the methods inside that interface

How to use Adapter Classes Now our handler class will inherit from adapter class public class EventsEx extends MouseMotionAdapter { …. } Due to inheritance, all the methods of the adapter class will be available inside our handler class Since adapter classes has already provided definitions with empty bodies. We do not have to provide implementations of all the methods again We only need to override our method of interest.

Adapter Classes

Example Code: Adapter Classes Modification of EventsEx // File EventsEx.java, Code listed in Handout section 13.1 …….. public class EventsEx extends WindowAdapter { JFrame f; JLabel coord; public void initGUI () { ……. // set layouts f.addWindowListener(this); ……. }//end initGUI

Example Code: Adpater Classes Modification of EventsEx.java // Event Handler for WindowListener public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } public static void main (String args[ ]){ EventsEx ex = new EventsEx(); } // end class

Example Code: Window Exit Handler Modification of EventsEx.java Output When user closes the window, Message would be displayed After pressing Ok button Program will exit

Last Code Example We have inherited from WindowAdapter What if we want to use MouseMotionAdpater as well ? or what if our class already inherits form some other class ? Problem But, Java allows single inheritance Solution Use Inner Classes

Inner Classes

Inner Classes A class defined inside another class Outer Class

Inner Classes Inner class can access the instance variables and members of outer class Outer Class GUI class (contains GUI creation code) tf is a JTextField Handler class contains event handling code tf is accessible here Inner class

Inner Classes It can have constructors, instance variables and methods, just like a regular class Generally used as a private utility class which does not need to be seen by others classes

Handling Window Events Inner Classes Handling Window Events

Example Code: Inner Classes Handling Window Events // File EventsEx.java, Code listed in Handout section 13.2 …….. public class EventsEx { JFrame f; JLabel coord; //inner class private class WindowHandler extends WindowAdapter { // Event Handler for WindowListener public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } } // end of WindowHandler

Example Code: Inner Classes Handling Window Events public void initGUI () { ……. // set layouts WindowHandler handler = new Window Handler (); f.addWindowListener(handler); ……. }//end initGUI // main method } // end of EventsEx class

Handling Window & Mouse Motion Events Inner Classes Handling Window & Mouse Motion Events

Handling Window Events Inner Classes Handling Windows & Mouse motion events GUI class Handling Mouse Events Handling Window Events

Example Code: Inner Classes Handling Window & MouseMotion Events // File EventsEx.java, Code listed in Handout section 13.3 …….. public class EventsEx { JFrame f; JLabel coord; //inner class private class WindowHandler extends WindowAdapter { // Event Handler for WindowListener public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } } // end of WindowHandler

Example Code: Inner Classes Handling Window Events private class MouseHandler extends MouseMotionAdapter { // Event Handler for mouse motion events public void mouseMoved (MouseEvent me) { int x = me.getX(); int y = me.getY(); coord.setText(“Moved at [" + x + "," + y + "]” ); } } // end of MouseHandler

Example Code: Inner Classes Handling Window Events public void initGUI () { ……. // set layouts WindowHandler wHandler = new WindowHandler (); f.addWindowListener(wHandler); MouseHandler mHandler = new MouseHandler(); f.addMouseMotionListener(mHandler); ……. }//end initGUI //main method } // end EventsExclass

Why Not Outer Class? Event Generator class ….. JFrame frame; tf is not accessible Event Generator class ….. JFrame frame; JTextField tf; Action Event Handler ………. String s = tf.getText(); ……….. Window Event Handler

Anonymous Inner Classes

Anonymous Inner Classes Has no name Same as inner class in capabilities much shorter difficult to understand

Named vs. Anonymous Objects String s = “hello”; System.out.println(s); “hello” has a named reference s. Anonymous System.out.println(“hello”);

Example Code: Anonymous Classes // File EventsEx.java, Code listed in Handout section 13.4 …….. public class EventsEx { JFrame f; JLabel coord;

Example Code: Anonymous Classes public void initGUI () { ……. // set layouts //anonymous inner class f.addWindowListner (new WindowAdapter ( ) { public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } ); ……. }//end initGUI

Summary of Handling Events Approahces

Approaches for Handling Events By implementing Interfaces By extending from Adapter classes To implement the above two techniques we can use Same class (putting event handler & generator in one class) Separate class Outer class Putting event handlers & generator in two different classes Inner classes Anonymous Inner classes