1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.

Slides:



Advertisements
Similar presentations
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Advertisements

Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
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
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.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Chapter 10: Inheritance and Polymorphism
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.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
3461 Sequential vs. Event-driven Programming Reacting to the user.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
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. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
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.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
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.
Event Handling and Listeners in SWING The practice of event handling.
Object Oriented Programming in Java Habib Rostami Lecture 10.
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.
IB103 Week 9 Free Standing Programs Chapter 18. Applets vs. Applications Applications are free standing See Program “Greeting” which prints out Hello.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inner Classes.
CSC 205 Programming II Lecture 5 AWT - I.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Chapter 14 Event-Driven Programming
Inner Classes 27-Dec-17.
CompSci 230 S Software Construction
CHAPTER Reacting to the user.
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Event Handling Chapter 2 Objectives
Chapter 11: Inheritance and Polymorphism
GUI Event Handling Nithya Raman.
Event-driven programming for GUI
Inner Classes 29-Nov-18.
Web Design & Development Lecture 13
Chapter 16 Event-Driven Programming
Java Inner Classes.
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Programming Graphical User Interface (GUI)
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
Inner Classes 25-Oct-19.
Presentation transcript:

1 DemoBasic_v3, DemoBasic_v4 JButton JLabel

2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame constructor: toggleButton.addActionListener( ); NOTICE!!!!! The parameter must be an instance of an ActionListener How do we define an ActionListener ?

3 Creating an ActionListener How do we define an ActionListener ? –ActionListener is an interface –In fact, all of the various listeners (for all of the various types of events) are interfaces –Recall that an interface cannot be instantiated What can we do? –Interfaces can be implemented –The compiler enforces the rule that if a class implements an interface, it must provide bodies for all of the methods defined in the interface

4 Example of a Class Implementing an Interface public abstract class WindowAdapter implements WindowListener { void windowActivated(WindowEvent we) {} void windowClosed(WindowEvent we) {} void windowClosing(WindowEvent we) {} void windowDeactivated(WindowEvent we) {} void windowDeiconified(WindowEvent we) {} void windowIconified(WindowEvent we) {} void windowOpened(WindowEvent we) {} }

5 More on this example… So the class WindowAdapter implements the WindowListener interface… –Compiler will enforce rule that the class must provides a body for each of the methods defined in the interface –But the compiler will allow a class to define all of the bodies to be empty!!! Q1: What do we call such a class? Q2: Why would we want such a class anyway?

6 More on Interfaces Q1: What do we call such a class? –An adapter class –The Java Foundation Classes (JFC) include several of these Q2: Why would we want such a class anyway? –we can extend an adapter class and override selected methods –it can be easier to do this with a adapter class from JFC than to implement the interface ourselves

7 Listeners and Corresponding Adapters Listener interface (# methods) Adapter class WindowListener (7) WindowAdapter ActionListener (1) not defined Later we’ll discuss… KeyListener (3) KeyAdapter MouseListener (5) MouseAdapter MouseInputListener (7)* MouseInputAdapter ItemListener (1) not defined FocusListener (2) FocusAdapter * MouseInputListener combines MouseListener and MouseMotionListener

8 Creating an ActionListener ActionListener is an interface; it cannot be instantiated Option A : create a named inner class that implements the interface e.g., DemoBasic_v3 Option B : make the sub-class of JFrame implement the interface e.g., DemoBasic_v4 Option C : create an anonymous inner class that implements the interface e.g., DemoVeryBasic_v3

9 A : Using a Named Inner Classes (1) What is an inner class? A nested class is a class that is a member of another class. A non-static nested class is called an inner class. [The Java TM Tutorial, “Implementing Nested Classes”]

10 A : Using a Named Inner Classes (2) To make use of a named inner class: define a class within the class definition for the JFrame sub-class –e.g., in DemoBasic_v3, the class MyListener is defined within the class DemoBasicFrame design the class to implement ActionListener

11... public class NameOfProgramFrame extends JFrame { public... private... public class NameOfProgramFrame() {} class MyListener implements ActionListener { public void actionPerformed(ActionEvent ae {... } private... public... } Declare variables (“fields”, “attributes”) Constuctor class must implement all of the ActionListener methods (there is only one, though) Other methods Inner Class

12 A : Registering an ActionListener Suppose an ActionListener can be instantiated from a named inner class To register it, invoke the following: toggleButton.addActionListener( new MyListener() ); **This was done in DemoBasic_v3

13 B : Using the JFrame sub-class In all of these examples, DemoBasicFrame was defined as a sub- class of JFrame What prevents us from also making DemoBasicFrame an ActionListener ?

14 B : Using the JFrame sub-class How to make DemoBasicFrame an ActionListener: –need to assert that it implements the ActionListener interface use keyword implements in class definition –need to ensure that method bodies are provided for all of the methods defined in the interface the compiler will ensure this –e.g., in DemoBasic_v4, the class DemoBasicFrame implements ActionListener

15 import... public class NameOfProgram { public static void main(String[] args) { } public class NameOfProgramFrame extends JFrame implements ActionListener { } Our GUI class implements the methods of the ActionListener listener

16... public class NameOfProgramFrame extends JFrame implements ActionListener { public... private... public class NameOfProgramFrame() {} public void actionPerformed(ActionEvent ae) {} public... private... } Declare variables (“fields”, “attributes”) Constructor Must implement all of the ActionListener methods (there is only one, though) Other methods

17 B : Registering an ActionListener Suppose the constructor of the JFrame sub-class also instantiates an ActionListener To register it, invoke the following (from within constructor): toggleButton.addActionListener(this); **This was done in DemoBasic_v4

18 C : Using an Anonymous Inner Class What is an anonymous inner class? an inner class that is declared without a name Syntax: new () { } This can be used in places where an instance is needed

19 C : Using an Anonymous Inner Class Finally, we revisit DemoVeryBasic_v3 : [slide 27] Recall we added a window listener to DemoVeryBasicFrame, the argument to the addWindowListener method was: new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } } this defines a WindowListener using an anonymous inner class

20 C : Using an Anonymous Inner Class We can do the same for ActionListener : new ActionListener() { public void actionPerformed(ActionEvent ae) {... } } this defines an ActionListener **This was done in DemoBasic_v5

21 Discussion To make use of a particular listener: –should I extend its adapter class, or –should I implement the interface? If I implement the interface, which class should do it? –the JFrame child class, –a named inner class, or –an anonymous inner class? If I extend the adapter, should I do it with –an anonymous inner class, or –a named inner class?

22 Discussion When extending adapter classes: –need to provide code only for methods that are needed –need to define an additional inner class (either named or anonymous) –if you use an anonymous inner class, your code can be difficult to read some say use sparingly, only for classes with one or two short methods; some say don’t use at all –if you use a named inner class, you need to instantiate an additional object –you can only extend one adapter class (compare with next option) Java does not include multiple inheritance (unlike C++ or Eiffel)

23 Discussion When implementing interfaces: –need to provide code for all of the methods whether you need them or not can define method bodies to be empty, though –don’t need to define an additional inner class; you can use the JFrame subclass –a class can implement many different listeners

24 Whether to Extend Adapters or Implement Listeners… Largely a matter of personal choice Sample applications in this course will do both –in DemoBasic_v3, _v4, we implemented the ActionListener interface note that no ActionAdapter class is defined in Java –DemoVeryBasic_v3 extended WindowAdapter this was done using an anonymous inner class you could easily define a named inner class instead (in fact, the code probably would be more readable this way)