Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Problem with AWT listeners

Started by King of Men Apr 13, 2005 at 9:04 PM 3 replies 800+ views
Original Post
King of Men
King of Men
I am trying to write some internal button classes; since they are going to share a lot of functionality, I derive them from a single superclass. Unhappily, this seems to cause me problems with the MouseClicked method. Here is my code :

   class MyButton extends Container implements MouseListener {
      private Label nameLabel;

      public MyButton (String tex) {
	 nameLabel = new Label(tex, Label.CENTER);
	 setLayout(null);
	 addMouseListener(this);
      }

      public void mouseClicked  (MouseEvent e) {
        System.out.println("mouseClicked!");
      }

      public void mouseEntered  (MouseEvent e) {
        System.out.println("mouseEntered!");
      }

      public void mouseExited   (MouseEvent e) {}
      public void mousePressed  (MouseEvent e) {}
      public void mouseReleased (MouseEvent e) {}
   }

   final class DamageViewButton extends MyButton {

      public DamageViewButton () {
	 super("D");
      }

      public void mouseClicked  (MouseEvent e) {
	 System.out.println("I hear ya!");
	 currModuleViewType = VIEWDAMAGE;
	 updateModuleView();
      }
   }


When the mouse enters the button, I get the message "mouseEntered" just as I should. But on clicking, I get neither "mouseClicked" not "I hear ya!" Does anyone know what the problem might be? Should I not overwrite listener-interface methods?
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
pirate_dau
pirate_dau
I have thought for a while on how to respond to this... I think basically you have a design problem (it does seem strange that neither is executed, but I don't think it's worth going into why that is). Does it make sense for your base class to have any implementation of MouseListener, though? Add your mouse listener where it makes sense -- in the specific class that extends MyButton. If you have several buttons that share WindowListener implementations, then make that its own class ala

public class MyButtonMouseListener extends MouseAdapter {     // Your shared implementation methods here}


This doesn't really answer your question, but I'm pretty sure it would solve your problem.
King of Men
King of Men
Well, I was thinking that I didn't want to clutter up my code with more than one empty mouseEntered, etc, methods, but I suppose that's not so important. More weirdness to come, though : I moved the MouseListener implementation to the subclass, and still no mouseClicked! When I extend Container directly, though, it works as it should.

This is Deep Weirdness. I guess I'll just do it the non-elegant way and repeat some code.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
King of Men
King of Men
Um, duh, now I feel stupid. I omitted some "non-relevant" initialisation code, where I set the bounds of the nameLabel to equal the bounds of the button. So, naturally, when I click the button area, the MouseEvent gets sent to the Label, which, of course, has no listener attached, so nothing happens.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
pirate_dau
pirate_dau
extending MouseAdapter instead of implementing MouseListener will hide those empty methods from you. MouseAdapter is basically a blank implementation of MouseListener.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.