Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Event handling

 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.0 doesn't suppot inner class. It that the reason?
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not it for me. I was using 1.2. Good call though.
Sean
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic