Gui Programming & Event Handling

Gui Programming & Event Handling GUI Programming   Adding componets to the window (Node to the Scene) Reacting to user actions  or event handl...
Author: Ethelbert Short
4 downloads 1 Views 379KB Size
Gui Programming & Event Handling

GUI Programming 



Adding componets to the window (Node to the Scene) Reacting to user actions 

or event handling

Example: adding a button that will change the background of our “window”

// import statements public class MyFXProg extends Application{ public static void main(String[ ] args) { MyFXProg.launch(args); } public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane();

Simple GUI Example

Button btn = new Button(); btn.setText("Push Me'"); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); } } // end of program

// import statements public class MyFXProg extends Application{ public static void main(String[ ] args) { MyFXProg.launch(args); } public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane();

Simple GUI Example

Button btn = new Button(); btn.setText("Push Me"); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 250, 150)); Not shown in the primaryStage.show(); future slides. } Not enough room!! } // end of program

public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane();

Simple GUI Example When the button is pushed, tell the PushMeListener object

Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction( new PushMeListener( ) ); root.getChildren().add(btn);

primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); }

The Listener public class PushMeListener implements EventHandler { public void handle(ActionEvent actEvt) { System.out.println( actEvt.getSource( ).toString( ) ); } }

Simple class that will print out the name of the class where the action took place.

The Listener: a little more interesting public void handle(ActionEvent actEvt) { Scene scene = ((Button) actEvt.getSource() ).getScene(); if (scene.getFill() instanceof Color ){ Color color = (Color) scene.getFill(); if( color.equals(Color.BLUEVIOLET)) { System.out.println( "it was blue violet...changing to corn flower blue"); scene.setFill(Color.CORNFLOWERBLUE); } else { System.out.println( "it wasn't blue violet...changing to blue violet"); scene.setFill(Color.BLUEVIOLET); } } else System.out.println( "it's NOT a Color"); }

The Listener: a little more interesting

The Listener: a little more interesting

The Listener: a little more interesting

The Listener: a little more interesting

Now if we push the button what will happen?

The Listener: a little more interesting public void handle(ActionEvent actEvt) { Scene scene = ((Button) actEvt.getSource() ).getScene(); if (scene.getFill() instanceof Color ){ Color color = (Color) scene.getFill(); if( color.equals(Color.BLUEVIOLET)) { System.out.println( "it was blue violet...changing to corn flower blue"); scene.setFill(Color.CORNFLOWERBLUE); } else { System.out.println( "it wasn't blue violet...changing to blue violet"); scene.setFill(Color.BLUEVIOLET); } } else System.out.println( "it's NOT a Color"); }

The Listener: a little more interesting

Now if we push the button what will happen?

General Idea Event EventCreater Creater

1

can have many

*

Event EventListener Listener

addListener( listener ) removeListener( listener ) notifyListeners( )



implemented by most GUI classes  allows the “handling” of the event to be separated from the source (creator) of the event

General Idea Event EventCreater Creater

1

can have many

*

Event EventListener Listener

addListener( listener ) removeListener( listener ) notifyListeners( ) 

Something happens to Event Creater(button push, mouse click...)

General Idea Event EventCreater Creater

1

can have many

*

Event EventListener Listener

addListener( listener ) removeListener( listener ) notifyListeners( ) 



Something happens to Event Creater(button push, mouse click...) Event Creater sends a message (event) to each of the listeners, by calling a function for the listener

General Idea Event EventCreater Creater

1

can have many

*

Event EventListener Listener

addListener( listener ) removeListener( listener ) notifyListeners( ) 





Something happens to Event Creater(button push, mouse click...) Event Creater sends a message (event) to each of the listeners, by calling a function for the listener the Event Listener can do what needs to be done in the called function ( call other functions, calculate something...)

Adding a second listener public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane(); Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.setOnAction(new PushMeListener2() ); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); }

The Second Listener public class PushMeListener2 implements EventHandler { @Override public void handle(ActionEvent actEvt) { Button button = ((Button) actEvt.getSource() ); button.setText("Just Pushed"); } }

Adding a second listener public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane(); Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.setOnAction(new PushMeListener2() ); root.getChildren().add(btn);

Hmmm....what primaryStage.setScene(new Scene(root, 250, 150)); happened here? primaryStage.show(); }

Adding a second listener public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane(); Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.setOnAction(new PushMeListener2() ); root.getChildren().add(btn); setOnAction( ) only “keeps” the primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show();last one added }

Adding a second listener public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane(); Button btn = new Button(); btn.setText("Push Me"); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); }

Adding a second listener public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); StackPane root = new StackPane(); Button btn = new Button(); btn.setText("Push Me"); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); root.getChildren().add(btn);

}

primaryStage.setScene(new Scene(root, Ah, now that is 250, 150)); primaryStage.show(); much better.

Implements vs Extends public class PushMeListener

implements EventHandler { public void handle(ActionEvent actEvt) { System.out.println( actEvt.getSource( ).toString( ) ); } }

Implements vs Extends public class PushMeListener

implements EventHandler { public void handle(ActionEvent actEvt) { System.out.println( actEvt.getSource( ).toString( ) ); } ????

} Who is the parent class?

extends PushMeListener

Implements vs Extends public class PushMeListener

implements EventHandler { public void handle(ActionEvent actEvt) { System.out.println( actEvt.getSource( ).toString( ) ); } Object

} Who is the parent class?

extends PushMeListener

Implements vs Extends public class PushMeListener

implements EventHandler { public void handle(ActionEvent actEvt) { System.out.println( actEvt.getSource( ).toString( ) ); } }

Java only allows single inheritance

Object extends PushMeListener

EventHandler implements

Implements vs Extends public class PushMeListener

implements EventHandler { public void handle(ActionEvent actEvt) { System.out.println( actEvt.getSource( ).toString( ) ); } }

Java only allows single inheritance both mechanisms allow polymorphism

Object extends PushMeListener

EventHandler implements

Implements vs Extends public interface EventHandler extends EventListener { public void handle(T event); } What do you see about an interface?

Implements vs Extends public interface EventHandler extends EventListener { public void handle(T event); } What do you see about an interface?

- no function definitions - can use inheritance of other interfaces - can use “templates” in Java called generics

Interface public interface EventHandler extends EventListener { public void handle(T event); } What do you see about an interface? very similar to Abstract class

- no function definitions - can use inheritance of other interfaces - can use “templates” in Java called generics

Chapter 14

Abstract class VS Interface

? Shape

Star

draw() erase()

Triangle

draw() erase()

Shape

Star

Circle

Interfaces are similar to abstract classes

Abstract class VS Interface Interface

Abstract Class 

allows method definitions



no method definition, only declaration

Abstract class VS Interface Interface

Abstract Class 



allows method definitions

becomes the super class





no method definition, only declaration doesn't change/affect class hierarchy

Abstract class VS Interface Interface

Abstract Class 

allows method definitions



becomes the super class



allows member variables







no method definition, only declaration doesn't change/affect class hierarchy allows only final static variables

Abstract class VS Interface Interface

Abstract Class 

allows method definitions



becomes the super class





allows member variables

methods can use any access





no method definition, only declaration doesn't change/affect class hierarchy



allows only final static variables



methods can only be public

Abstract class VS Interface Interface

Abstract Class 

allows method definitions





no method definition, only declaration doesn't change/affect class hierarchy



becomes the super class



allows member variables



allows only final static variables



methods can use any access



methods can only be public

Both allow the creation of a variable that can refer to subclass objects and perform polymorphism

Another Example 

In a game there are “units” that can shoot some kind of weapon that will do damage to “targets” (other units, buildings, trees....)

Person

Soldier

???

Vehicle fireWeapon( Target target )

Tank

???

Flyer

fireWeapon( Target target )

WarPlane

???

fireWeapon( Target tar )

Another Example 

We would like to use polymorphism to call the fireWeapon method, but they come from different parents.

Person

Soldier

???

Vehicle fireWeapon( Target target )

Tank

???

Flyer

fireWeapon( Target target )

WarPlane

???

fireWeapon( Target tar )

Another Example 

We could make an abstract class as a parent Shooter abstract fireWeapon( Target target )

Person

Soldier

???

Vehicle fireWeapon( Target target )

Tank

???

Flyer

fireWeapon( Target target )

WarPlane

???

fireWeapon( Target tar )

Another Example 

....but what is the problem with this solution? Shooter abstract fireWeapon( Target target )

Person

Soldier

???

Vehicle fireWeapon( Target target )

Tank

???

Flyer

fireWeapon( Target target )

WarPlane

???

fireWeapon( Target tar )

Another Example



What is the problem? Every sub-class needs to override the abstract method. Shooter abstract fireWeapon( Target target )

Person

fireWeapon( Target target )

Vehicle

fireWeapon( Target target )

Flyer

fireWeapon( Target tar )

Soldier

fireWeapon( Target target )

Tank

fireWeapon( Target target )

WarPlane

fireWeapon( Target tar )

???

???

???

Another Example



If we use interface, only the classes(and their child) would need to implement the method Shooter

im

m e l p

Person

Soldier

???



ts n e

fireWeapon( Target target )

Vehicle

fireWeapon( Target target )

Tank

???

Flyer

fireWeapon( Target target )

WarPlane fireWeapon( Target tar )

???

Using the interface 

in other code, we could use the interface to access the instances polymorphically

Collection shooters ; // filled with shooting objects for (Shooter shooter : shooters ) shooter.fireWeapon( target );

We Wedon't don'tneed needtotoknow know exactly exactlywhat whattype typeofofobject object we wehave. have. We Wejust justneed needtotoknow knowthat thatitit can canfireWeapon fireWeapon

Using the interface 

in other code, we could use the interface to access the instances polymorphically

Collection shooters ; // filled with shooting objects for (Shooter shooter : shooters ) shooter.fireWeapon( target );



the advantage here is that new types of “units” (boats, robots....) can be added and the code won't need to change.

Good Software Design 

Separate what changes from what doesn't change  

often the interface should not change (public things) the implementation will change (maybe from class to class) 

implementation: how the method executes

Good Software Design 

Separate what changes from what doesn't change  

often the interface should not change (public things) the implementation will change (maybe from class to class) 

implementation: how the method executes What is changing?

What is not changing?

Good Software Design 

Separate what changes from what doesn't change  

often the interface should not change (public things) the implementation will change (maybe from class to class) 

implementation: how the method executes What is changing?

What is not changing?

behavior of the function

funtion purpose function idea function name

Good Software Design 

Separate what changes from what doesn't change  

often the interface should not change (public things) the implementation will change (maybe from class to class) 



implementation: how the method executes

Program for the interface, not the implementation

Good Software Design 

Separate what changes from what doesn't change  

often the interface should not change (public things) the implementation will change (maybe from class to class) 





implementation: how the method executes

Program for the interface, not the implementation Design Patterns - Design Patterns: Great Book on good Software Design

public class MyFXProg extends Application implements EventHandler{ Stage myStage; public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } public void start(Stage stage) { myStage = stage; stage.setTitle("Hello World!");

Changing our old example....

Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); btn.addEventHandler(ActionEvent.ACTION, this);

public class MyFXProg extends Application implements EventHandler{ Stage myStage; public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } public void start(Stage stage) { myStage = stage; stage.setTitle("Hello World!");

Changing our old example.... Now we can “listen” to ourself

Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); btn.addEventHandler(ActionEvent.ACTION, this);

public class MyFXProg extends Application implements EventHandler{ Stage myStage; public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } public void start(Stage stage) { myStage = stage; stage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); btn.addEventHandler(ActionEvent.ACTION, this);

public class MyFXProg extends Application implements EventHandler{ Stage myStage; public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } public void start(Stage stage) { myStage = stage; stage.setTitle("Hello World!");

This is nice because we can see what the listener will do without needing to look in another Class file

Button btn = new Button(); btn.setText("Push Me"); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); btn.addEventHandler(ActionEvent.ACTION, this);

public class MyFXProg extends Application Stage myStage;

Using Inner Classes

public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener3()); } // end of start class PushMeListener implements EventHandler { … } class PushMeListener2 implements EventHandler { … } class PushMeListener3 implements EventHandler { … } } // end of MyFXProg class

public class MyFXProg extends Application Stage myStage;

Using Inner Classes

public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); Advantages? btn.addEventHandler(ActionEvent.ACTION, new PushMeListener3()); } // end of start Disadvantages? class PushMeListener implements EventHandler { … } class PushMeListener2 implements EventHandler { … } class PushMeListener3 implements EventHandler { … } } // end of MyFXProg class

public class MyFXProg extends Application Stage myStage;

Using Inner Classes

public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction(new PushMeListener() ); btn.addEventHandler(ActionEvent.ACTION, new PushMeListener2() ); Advantages? btn.addEventHandler(ActionEvent.ACTION, new PushMeListener3()); - easy to find..in same file } // end of start

- have access to the outer class's things class PushMeListener implements EventHandler {…}

class PushMeListener2 implements EventHandler { … } Disadvantages? class PushMeListener3 implements EventHandler { … } } // end of MyFXProg class

-can't be used by other classes (not that important)

Most Common: Using Anonymous Inner Classes public class MyFXProg extends Application Stage myStage; public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction( new EventHandler( ) { public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } } ); … // rest of start method } // end of MyFXProg class

Most Common: Using Anonymous Inner Classes public class MyFXProg extends Application Stage myStage; public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction( new EventHandler( ) { public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } } ); function call … // rest of start method } // end of MyFXProg class

Most Common: Using Anonymous Inner Classes public class MyFXProg extends Application Stage myStage; public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction( new EventHandler( ) { public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); declares a class } without a name } ); that implements … // rest of start method EventHandler } // end of MyFXProg class

interface

Most Common: Using Anonymous Inner Classes public class MyFXProg extends Application Stage myStage;

Chapter 16

public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction( new EventHandler( ) { public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } class definition } ); (body) … // rest of start method } // end of MyFXProg class

Most Common: Using Anonymous Inner Classes public class MyFXProg extends Application Stage myStage;

Advantages? Disadvantages?

public void start(Stage stage) { Button btn = new Button(“Push Me”); btn.setOnAction( new EventHandler( ) { public void handle(ActionEvent actEvt) { myStage.setTitle("Button Pushed"); } } ); … // rest of start method } // end of MyFXProg class

Most Common: Using Anonymous Inner Classes public class MyFXProg extends Application Stage myStage;

Advantages? - listener code is near to the button creation

public void start(Stage stage) { Button btn = new Button(“Push Me”); Disadvantages? btn.setOnAction( new EventHandler( ) { - can make code harder public void handle(ActionEvent actEvt)to{ read if listener is long myStage.setTitle("Button Pushed");- no arguments can be } passed to constructor } ); - listener object can … // rest of start method only be used once } // end of MyFXProg class

Inner Classes 

class created inside of another class



can access all the things of the outer



when compiled it will create: OuterClassName$InnerClassName.class

Anonymous Inner Class Anonymous inner class is a special kind of inner class An anonymous inner class must extend a superclass or implement an interface 

but it cannot have an explicit extends or implements clause.

Anonymous Inner Class Anonymous inner class is a special kind of inner class An anonymous inner class must extend a superclass or implement an interface 

but it cannot have an explicit extends or implements clause.

An anonymous inner class must implement all the abstract methods in the superclass or in the interface. An anonymous inner class always uses the no-arg constructor of its superclass to create an instance.  When compiled they will create class like this: OuterClassName$1.class, OuterClassName$2.class

FX Event Handling 



What happens with an event? if we have this scene, it would have a scene graph like this:

FX Event Handling if the mouse button is clicked on the triangle object  a MouseEvent.MOUSE_CLICKED event will be created 

FX Event Handling if the mouse button is clicked on the triangle object  a MouseEvent.MOUSE_CLICKED event will be created  it will travel from the Stage down to the Triangle  in any of the objects along the path a filter could be activated 

FX Event Handling if there is a mouse button is clicked on the triangle object  a MouseEvent.MOUSE_CLICKED event will be created  it will travel from the Stage down to the Triangle  in any of the objects along the path a filter could be activated  it will then travel up from the Triangle back up to the Stage  again in any of the objects along the path a handler could be activated 

FX Event Handling it will travel from the Stage down to the Triangle  in any of the objects along the path a handler could be activated  EventFilter “catch” events traveling down  it will then travel up from the Triangle back up to the Stage  again in any of the objects along the path a handler could be activated  EventHandler “catch” events traveling up 

FX Event Handling 

The traveling up and down by the event can be stopped at any place in the tree by calling the consume method on the event

Lets change this program.... We will set up an EventFilter at the root that will filter the event before it gets to the button. class MyFXProgram extends Application{ Stage priStage; public void start(Stage primaryStage) { priStage = primaryStage; primaryStage.setTitle("Hello World!"); Button btn = new Button( ); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() { ... }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); }

We will set up an EventFilter at the root that will filter the event before it gets to the button. public void start(Stage primaryStage) { priStage = primaryStage; primaryStage.setTitle("Hello World!"); Button btn = new Button( ); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() {

... });

StackPane root = new StackPane(); root.addEventFilter(MouseEvent.ANY, new EventHandler() { public void handle(MouseEvent event) { System.out.println("Handling event " + event.getEventType()); event.consume(); } now the event }); is intercepted root.getChildren().add(btn); before it gets primaryStage.setScene(new Scene(root, 250, 150)); to button primaryStage.show(); }

We will set up an EventFilter at the root that will filter the event before it gets to the button. public void start(Stage primaryStage) { priStage = primaryStage; primaryStage.setTitle("Hello World!"); Button btn = new Button( ); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() {

... });

StackPane root = new StackPane(); root.addEventFilter(MouseEvent.ANY, new EventHandler() { public void handle(MouseEvent event) { System.out.println("Handling event " + event.getEventType()); // event.consume(); } }); now the event root.getChildren().add(btn); can be acted on by both primaryStage.setScene(new Scene(root, 250, 150)); the root and button primaryStage.show(); }

We will set up an EventFilter at the root that will filter the event before it gets to the button. public void start(Stage primaryStage) { priStage = primaryStage; primaryStage.setTitle("Hello World!"); Button btn = new Button( ); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() {

... });

StackPane root = new StackPane(); root.addEventHandler(MouseEvent.ANY, new EventHandler() { public void handle(MouseEvent event) { System.out.println("Handling event " + event.getEventType()); event.consume(); } now the event will be }); “caught” after the root.getChildren().add(btn); button, as it is going primaryStage.setScene(new Scene(root, 250, 150)); back to the Stage primaryStage.show(); }

Convenience method from Node public void start(Stage primaryStage) { priStage = primaryStage; primaryStage.setTitle("Hello World!"); Button btn = new Button( ); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() {

...

});

StackPane root = new StackPane(); root.addEventHandler(MouseEvent.ANY, new EventHandler() { public void handle(MouseEvent event) { System.out.println("Handling event " + event.getEventType()); This method will set up event.consume(); a listener using the } Handling path (upward) }); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); }

Event Listening 





while you are confused about the difference between filters and handlers you can use the Convenience methods: setOnX for example button.setOnMouseEnter( … ) which will set up an handler Event Filters and Handlers allow events to be “caught” by different part of the program More on this topic: http://docs.oracle.com/javafx/2/events/jfxpub-events.htm

Topics Covered 

basics of event handling 

listeners



registering listeners



Event traveling up and down the Scene Graph



Interfaces (and adapters......this is covered later)



Inner Classes 

Anonymous inner classes

This is enough to get you started and for the lab next week........

Adapters 

Adapters: a class that implements an interface but overrides all the interface methods with empty methods

interface MaFan { void method1( ); void method2( ); void method3( ); void method4( ); void method5( ); void method6( ); void method7( ); }

public class MaFanAdapter implements MaFan { void method1( ) { } void method2( ) { } void method3( ) { } void method4( ) { } void method5( ) { } void method6( ) { } void method7( ) { } }

Adapters 



Adapters: a class that implements an interface but overrides all the interface methods with empty methods hmm......seems pretty strange. What would be the advantage of this? interface MaFan { void method1( ); void method2( ); void method3( ); void method4( ); void method5( ); void method6( ); void method7( ); }

public class MaFanAdapter implements MaFan { void method1( ) { } void method2( ) { } void method3( ) { } void method4( ) { } void method5( ) { } void method6( ) { } void method7( ) { } }

Adapters 



Adapters: a class that implements an interface but overrides all the interface methods with empty methods What would be the advantage of this? 

maybe a class only wants to use one of the methods

interface MaFan { void method1( ); void method2( ); void method3( ); void method4( ); void method5( ); void method6( ); void method7( ); }

public class MaFanAdapter implements MaFan { void method1( ) { } void method2( ) { } void method3( ) { } void method4( ) { } void method5( ) { } void method6( ) { } void method7( ) { } }

public class MyClass extends MaFanAdapter { void method6( ) { ........ } ..... }

Adapter Real Life Example from Swing GUI Library 

MouseListener is the interface for classes that want to “Listen” (respond) to mouse events.

interface MouseListener { void mouseClicked(MouseEvent e) ; void mouseEntered(MouseEvent e) ; void mouseExited(MouseEvent e) ; void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }

Real Life Example 



MouseListener is the interface for classes that want to “Listen” (respond) to mouse events. Any class that wants to be notified of mouse events must implement the MouseListener interface

interface MouseListener { void mouseClicked(MouseEvent e) ; void mouseEntered(MouseEvent e) ; void mouseExited(MouseEvent e) ; void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }

Real Life Example



MouseListener is the interface for classes that want to “Listen” (respond) to mouse events.

Any class that wants to be notified of mouse events must implent the MouseListener interface .... but once again, what if I only want to do something if the mouse is pressed on the object I care about



 if

I implement the interface, I must override all the methods

interface MouseListener { void mouseClicked(MouseEvent e) ; void mouseEntered(MouseEvent e) ; void mouseExited(MouseEvent e) ; void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }



Real Life Example

MouseListener is the interface for classes that want to “Listen” (respond) to mouse events.

Any class that wants to be notified of mouse events must implent the MouseListener interface .... but once again what if I only want to do something if the mouse is pressed on the object I care about



 if

I implement the interface, I must override all the methods

interface MouseListener { void mouseClicked(MouseEvent e) ; void mouseEntered(MouseEvent e) ; void mouseExited(MouseEvent e) ; void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }

class MouseAdapter implements MouseListener { void mouseClicked(MouseEvent e) { } void mouseEntered(MouseEvent e) { } void mouseExited(MouseEvent e) { } void mousePressed(MouseEvent e) { } void mouseReleased(MouseEvent e) { } ..... }

Real Life Example 

Now my class just overrides the one or two methods that I am concerned about

MouseAdapter MouseAdapter

MyClass MyClass

class MouseAdapter implements MouseListener { void mouseClicked(MouseEvent e) { } void mouseEntered(MouseEvent e) { } void mouseExited(MouseEvent e) { } void mousePressed(MouseEvent e) { } void mouseReleased(MouseEvent e) { } ..... }

mousePressed(MouseEvent e)

What is the only drawback of Adapters?

Real Life Example 

Now my class just overrides the one or two methods that I am concerned about

MouseAdapter MouseAdapter

class MouseAdapter { void mouseClicked(MouseEvent e) { } void mouseEntered(MouseEvent e) { } void mouseExited(MouseEvent e) { } void mousePressed(MouseEvent e) { } void mouseReleased(MouseEvent e) { } ..... }

ZappaClass ZappaClass

MyClass MyClass

Can't Can'tinherit inheritfrom from two classes two classes

What is the only drawback of Adapters?

Real Life Example 

Now my class just overrides the one or two methods that I am concerned about

ZappaClass ZappaClass

MyClass MyClass

MouseAdapter MouseAdapter EventEventHandler Handler

class MouseAdapter { void mouseClicked(MouseEvent e) { } void mouseEntered(MouseEvent e) { } void mouseExited(MouseEvent e) { } void mousePressed(MouseEvent e) { } void mouseReleased(MouseEvent e) { } ..... }

void mousePressed(MouseEvent e) { ... }

including another object 

There are two ways to include an object in another class 

composition: create an member object

public class MyClass extends ZappaClass { EventHandler ev = new EventHandler() myButton.addMouseListener( ev );

MouseAdapter ZappaClass ZappaClass MyClass MyClass

EventHandler

including another object 

There are two ways to include an object in another class  

composition: create an member object Anonymous Inner class: create an unnamed class

public class MyClass extends ZappaClass { myButton.addMouseListener ( new MouseAdapter( ) { public void mousePressed(....) { // function } } );

ZappaClass ZappaClass MyClass MyClass

MouseAdapter