Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adapter classes  RSS feed

 
Amit Sethi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain the statement for registering listener in init() method & why we are declaring refrence of A16 in MyMouseAdapter & MyMouseMotionAdapter.
& is there any good tutorial for adapter classes.


//page no. 664 complete refrence

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/* <applet code = "A16.class" width = 300 height = 300>
</applet>
*/


public class A16 extends Applet {

public void init() {
setBackground(Color.cyan);
addMouseListener(new MyMouseAdapter(this)); // this line is not clear
addMouseMotionListener(new MyMouseMotionAdapter(this));
}

}

class MyMouseAdapter extends MouseAdapter {

A1 a16;

public MyMouseAdapter(A16 a1) {
this.a16 = a16;
}

public void mouseClicked(MouseEvent me) {
a16.showStatus("mouse clicked");
}
}

class MyMouseMotionAdapter extends MouseMotionAdapter {

A16 a16;

public MyMouseMotionAdapter(A16 a16) {
this.a16 = a16;
}

public void mouseDragged(MouseEvent me) {
a16.showStatus("mouse Dragged");
}
}

Thanks & regards
Amit Sethi
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't really need a tutorial for the API adapter classes used here; they're just classes that implement the event listener interfaces with empty (do-nothing) methods. Then you can extend one and only override the methods you need, rather than implementing all of the methods in the interface yourself.

Why do the listeners have a reference to the A16 (applet) class? So that, when they are invoked, they can call a method on the applet. Without that reference, they wouldn't have any way of doing that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!