Lecture 15.1 Event Delegation.

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
UML Class Diagram: class Rectangle
Abstract Classes and Interfaces
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
SIGCSE’11 - Workshop #3Pedagogical Progressions for Teaching OOD Computer Science & Engineering Carl Alphonce A Progression of Events.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Inheritance in the Java programming language J. W. Rider.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
EVENTS Wiring together objects, code, and actions.
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.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming in Java Habib Rostami Lecture 10.
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.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Lecture 15 Basic GUI programming
Modular Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
JButton Chapter 11 Lecture 3.
Prototyping with Methods
Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East
Advanced Programming in Java
Advanced Programming in Java
Animating with EventTimer
Lecture 9.5 Event Delegation.
A First Look at GUI Applications
CompSci 230 S Programming Techniques
Prototyping with Methods
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
UML Class Diagram: class Rectangle
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Introduction to Event Handling
Inner Classes 29-Nov-18.
Interfaces.
Advanced Java Programming
Java Inheritance.
Advanced Programming in Java
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
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.
...that can get messy when we have lots buttons!
Presentation transcript:

Lecture 15.1 Event Delegation

EventButton Revisited import java.awt.event.*; import javax.swing.JButton; /** EventButton class (abstract) * Author: David Riley * Date: May, 2004 */ public abstract class EventButton extends JButton implements ActionListener { /** post: this is created with itself as an action listener * and getText() == s */ public EventButton(String s) { super(s); addActionListener(this); } public abstract void actionPerformed( ActionEvent e ); _____________ __________ ____________________

Why? (use abstract methods) Abstract Class An abstract class is any class that (generally) contains one or more abstract method(s). Another name for an abstract class is deferred class. public abstract class MyClass { . . . public abstract void deferredMethod(); } Abstract methods can be void or non-void, have a parameter list or be parameterless, be public, private or protected. Note that AbstractButton class is abstract but contains no abstract methods. The idea is simply to create a class that cannot be instantiated. Why? (use abstract methods) 1) 2) 3)

Interface Why? (use interfaces) An interface is a class (sort of) in which all methods are deferred. public interface MyInterface{ . . . public void deferredMethod(); } Note the syntax Interfaces may contain instance variables, but only if initialized, final, and static (stay tuned for static). Every method in an interface is implicitly deferred and public. Why? (use interfaces) Note that most interfaces do NOT include variables. 1) A class/interface "inherits" an interface using the word implements, instead of extends. Multiple interfaces can be inherited by the same class, but all deferred methods must be coded by the inheriting class.

javax.swing.JComponent javax.swing.AbstractButton {abstract} «constructor» + AbstractButton() «update» + void addActionListener( java.awt.event.ActionListener ) . . . «interface» java.awt.event.ActionListener «event handler» + void actionPerformed( java.awt.event.ActionEvent ) javax.swing.JButton «constructor» + JButton() + JButton( String ) . . . EventButton {abstract} «constructor» + EventButton( String ) «event handler» + void actionPerformed( java.awt.event.ActionEvent )

Event Delegation In Java events can be delegated to any object, given two requirements: 1) The delegate must belong to a class that implements ???Listener. 2) Must execute: eventGenerator.add???Listener( theDelegate ); import java.awt.event.*; //ActionEvent and ActionListener import javax.swing.*; //JFrame and JButton public class Driver implements ActionListener{ private JFrame win; private JButton hiGrampsButton; public Driver() { win = new JFrame("Button Delegation"); win.setBounds(20, 20, 200, 220); win.setLayout(null); win.setVisible(true); hiGrampsButton = new JButton("Hi"); hiGrampsButton.setBounds(50, 50, 100, 100); hiGrampsButton.addActionListener(this); win.add( hiGrampsButton, 0); win.repaint(); } public void actionPerformed(ActionEvent e) { System.out.println( "Hi Gramps!" );