This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with Event handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Event handling" Watch "Help with Event handling" New topic
Author

Help with Event handling

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ... I'm hoping someone can explain what I've done wrong in the following:
The example code is from "Java Certification Exam Guide" by Barry Boone. The original code works fine, it creates an applet and adds two custom canvas components as seperate threads. It then updates the canvas items each time the user clicks on the mouse, displaying the number of mouse-clicks.
The code uses the JDK 1.0 event handling model. I thought I could re-write it to use the new model by simply adding a MouseListener; however, the canvas threads are not being updated when I click on the applet.

Thanks in advance for any help.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Jane,
I think the problem stems from the use of the inner class to employ the MouseAdapter. If you try this, it works fine.
public class MyApplet extends Applet implements MouseListener{
boolean clicked;
int counter;

public void init() {
// add the applet as the MouseListener
addMouseListener(this);
add(new ClickCanvas( this ));
add(new ClickCanvas( this ));
}
public void mouseClicked(MouseEvent e) {
synchronized(this) {
clicked = true;
notifyAll();
}
counter++;
Thread.currentThread().yield();
clicked = false;
System.out.println("Count: " + counter);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
public void mouseReleased(MouseEvent e) {}
etc...
Notice, the applet itself becomes the MouseListener that you pass to the canvases. Hope this helps.
Sean
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Thanks Sean. Still not sure why the inner class doesn't work. Any ideas?
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
Alan H
Greenhorn

Joined: Nov 13, 2000
Posts: 6
Java 1.0 doesn't suppot inner class. It that the reason?
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
That's not it for me. I was using 1.2. Good call though.
Sean
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Alan, Sean ...
I'm using JDK 1.3 so don't think the inner class 'per se' is the problem.
I think, possibly, it's the way I'm registering the MouseListener. Most of the example code I've been able to find with seperate handlers defined, are assigning them to a component ie they create a button b then use b.addMouseListener(new MyHandler()). Other code, which doesn't use a seperate handler class, will add the listener to the applet itself with <code>addMouseListener(this)</code>.
I think using the code <code>addMouseListener(new MyMouseHandler())</code> doesn't properly register the listener with the applet and I can't figure out how to do this directly.
If you happen to trip over any similar examples, I'd appreciate a link back.
Thanks
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
GeeCON Prague 2014
 
subject: Help with Event handling