Question

Ok so I'm not sure why my MouseListener isn't working but I think it might be because I implemented both the ActionListener and MouseListener into the class. Would this cause the class to have an issue?

actionPerformed method:

public void actionPerformed(ActionEvent e){...
      ...
}

mouseClicked method:

public void mouseClicked(MouseEvent arg0) {
    ...
}

Is it because it's only listening to the actionPerformed method and never entering the MouseListener? If what I suspect is correct, how would I allow it to work together?

EDIT: I've narrowed it down to something is wrong in the MouseListener. It doesn't ever get any input from the mouse at all, do I have to specify the area it should be listening to?

public void mousePressed(MouseEvent arg0) {
        System.out.println("Inside timer is running");

        if(timer.isRunning() == true){
            System.out.println("Inside timer is running");
            Point p = arg0.getPoint();
        }
}
Was it helpful?

Solution

You ask:

Ok so I'm not sure why my MouseListener isn't working but I think it might be because I implemented both the ActionListener and MouseListener into the class. Would this cause the class to have an issue?

No, this should not affect things at all. Your problem most likely lies elsewhere in code not shown.

Having said this, I'd like to add that almost none of my GUI classes implement either of these or other listener interfaces, since I feel that this would be asking the class to have too much responsibility, making it harder to debug now or upgrade later. Instead I favor either anonymous inner classes that then call control methods, or a completely separate control/listener class(es).


Edit
I don't think that your posted code and text is adequate to allow us to understand your problem enough to answer it other than to say that the problem lies elsewhere. If you don't get a decent answer soon, consider creating and posting a Minimal, Complete, and Verifiable Example Program.


Edit 2
You state in comment:

I don't think I could post anymore code that may clear it up since this is a huge program. This class alone has 300 lines but I know everything else works just the MouseListener isn't working like it should

Up to you what you should do next, but if this were my code, and I were running into these problems, I would work some more on trying to isolate the problem, including refactoring my code so that that I eventually come up with the smallest critical code that reproduces the problem. You're probably coming here at too premature of a stage in your debugging, forcing you to post "what if" scenarios, and for us to shrug our shoulders and say, "who knows".


Edit 3
You ask:

do I have to specify the area it should be listening to?

You have to specify what component to listen to. MouseListeners listen to components. But again, this is little more than more "what if's" and "who knows"...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top