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.

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

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 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
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.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
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.
Java 212: Interfaces Intro to UML Diagrams. UML Class Diagram: class Rectangle +/- refers to visibility Color coding is not part of the UML diagram.
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.
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.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Object Oriented Programming.  Interface  Event Handling.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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 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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
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.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 6: Event-Driven Programming.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
CHAPTER Reacting to the user.
Web Design & Development Lecture 11
Advanced User Interfaces
A Quick Java Swing Tutorial
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Event-driven programming for GUI
Introduction to Event Handling
Chapter 16 Event-Driven Programming
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Programming Graphical User Interface (GUI)
Presentation transcript:

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 that combines object creation with the declaration of the class. This avoids naming a class, at the cost of only ever being able to create one instance of that anonymous class. This is handy in the AWT. CS3 - Events

Anonymous Class Syntax An anonymous class is defined as part of a new expression and must be a subclass or implement an interface The class body can define methods but cannot define any constructors. The restrictions imposed on local classes also apply new className( argumentList ) { classBody } new interfaceName() { classBody } CS3 - Events

Using Anonymous Classes public class Dog { private String breed; private String name; public Dog( String theBreed, String theName ) { breed = theBreed; name = theName; } public String getBreed() { return breed; } public String getName() { return name; } public int compareTo( Object o ) throws ClassCastException { Dog other = (Dog)o; int retVal = breed.compareTo( other.getBreed() ); if ( retVal == 0 ) retVal = name.compareTo( other.getName() ); return retVal; } // Dog CS3 - Events

Using Anonymous Classes public void PrintDogsByName( List dogs ) { List sorted = dogs; Collections.sort( sorted, new Comparator () { public int compare( Object o1, Object o2) { Dog d1 = (Dog)o1; Dog d2 = (Dog)o2; return d1.getName().compareTo( d2.getName() ); } ); Iterator i = sorted.iterator(); while ( i.hasNext() ) System.out.println( i.next() ); CS3 - Events

The Job of a Window Manager CS3 - Events

Event Driven Programming Programs respond to events that are generated outside the control of the program User types a key The left mouse button is pressed A CD is removed from the CD drive When an event occurs, it is handled by an event handler Event driven programming involves writing the handlers and arranging for the handler to be notified when certain events occur CS3 - Events

Event Handling Events are represented by objects that gives information about the event and identifies the event source Event sources are typically components, but other kinds of objects can also be event sources A listener is an object that wants to be notified when a particular event occurs An event source can have multiple listeners registered on it A single listener can register with multiple event sources CS3 - Events

Listeners In order for an object to be notified when a particular event occurs, the object Must implement the appropriate Listener interface Be registered as an event listener on the appropriate event source CS3 - Events

Swing Listeners Action Listener Type User clicks a button, presses return while typing in a text filed, or chooses a menu item ActionListener Users closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the move over a component MouseMotionListener A component becomes visible ComponentListener A component gets the keyboard focus FocusListener A table of list selection changes ListSelectionListener CS3 - Events

Window Closing A very common event directed towards a window is a close event The default behavior is to simply hide the JFrame when the user closes the window Normally we would want the program to terminate when the user closes the main window Two steps required to accomplish this Write an event handler for the close event that will terminate the program Register the handler with the appropriate event source CS3 - Events

WindowListener The WindowListener interface void windowActivated(WindowEvent e); void windowClosed(WindowEvent e); void windowClosing(WindowEvent e); void windowDeactivated(WindowEvent e); void windowDeiconified(WindowEvent e); void windowIconified(WindowEvent e); void windowOpened(WindowEvent e); A class that implements WindowListener must implement all of these methods! CS3 - Events

WindowAdapter A class that implements WindowListener The methods in this class are empty. The class exists as convenience for creating listener objects. To use the WindowAdapter class: Extend this class to create a WindowEvent listener Override the methods for the events of interest Create a listener object using the extended class and then register it with the Window. When an event occurs the appropriate method in the listener is invoked. CS3 - Events

The Result import javax.swing.*; Import java.awt.event.*; public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit ( 0 ); } ); win.setSize( 250, 150 ); win.show(); } // SwingFrame CS3 - Events

Buttons Buttons generate action events The ActionListener interface void actionPerformed(ActionEvent e); Note that there is no need for an ActionAdapter class Generally one ActionListener will be responsible for handling the events generated by a group of buttons You can tell which button got pressed using the event’s getActionCommand() method CS3 - Events

Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingFrame implements ActionListener { private JFrame win; public SwingFrame( String title ) { win = new JFrame( title ); win.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit ( 0 ); } ); win.getContentPane().setLayout( new FlowLayout() ); CS3 - Events

Example for ( int i = 0; i < 10; i++ ) { JButton b = new JButton( String.valueOf( i ) ); b.addActionListener( this ); win.getContentPane().add( b ); } win.pack(); win.show(); public void actionPerformed( ActionEvent e ) { System.out.println( "Button " + e.getActionCommand() + " was pressed " ); public static void main( String args[] ) { SwingFrame f = new SwingFrame( "My First GUI" ); } // SwingFrame CS3 - Events

StopWatch.java CS3 - Events

TimerLabel.java CS3 - Events

GUI Program Design The GUI provides a view of the program, it is clearly not the program Making the GUI code as independent of the program code is a good strategy Changes in the program do not necessarily change the GUI Different GUIs can be developed for the same program Debugging and maintaining both the GUI and the program code is easier CS3 - Events

Model-View-Controller The MVC pattern is commonly used to develop applications that have a GUI component Consists of three parts Model The program View The GUI Controller The event handling mechanism CS3 - Events

MVC Model View Controller The model passes its data to the view for rendering Model View The view determines which events Are passed to the controller The controller updates the model Based on the events received Controller CS3 - Events

MVC in Swing View Model Controller Program Logic The GUI The Application CS3 - Events

A Simple 4 Function Calculator CS3 - Events