Reputation: 97
I was wondering how to stop mousedMoved from being fired. I've been googling but can't find an answer. Is there a method for this? I'm using eclipse and went through the mouseevent methods but just can't find anything.
public class Drawing extends JPanel {
private ArrayList<Point> pointList;
private int counter = 0;
public Drawing() {
setLayout(new FlowLayout());
setBackground(Color.white);
pointList = new ArrayList<Point>();
addMouseListener(new MouseTrackerListener());
}
public void paintComponent(Graphics pen) {
super.paintComponent(pen);
for (int i = 0; i < pointList.size(); i++) {
Point p = pointList.get(i);
pen.fillOval(p.x, p.y, 10, 10);
}
}
private class MouseTrackerListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {
counter++;
if (counter % 2 != 0) {
addMouseMotionListener(new MouseTrackerListener());
} else {
System.out.println("Hi");
}
}
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
pointList.add(point);
repaint();
}
}
Upvotes: 0
Views: 667
Reputation: 4525
you can create a boolean
to toggle if it's on drawing status or not. you name the boolean
like isDrawingMode
so when you click the mouse.. you set it to false, if you click it again, it will become true;
all you have to do is to toggle the boolean isDrawingMode
when the mouse was clicked
so your mousemoved listener
will look like this
public void mouseMoved(MouseEvent e) {
if (!isDrawingMode) return; //if isDrawingMode is false, it will not trigger to draw
Point point = e.getPoint();
pointList.add(point);
repaint();
}
Upvotes: 1
Reputation: 2453
For Java
You could use a common base class for your listeners and in it, have a static method to turn the listeners on or off:
public abstract class BaseMouseListener implements ActionListener{
private static boolean active = true;
public static void setActive(boolean active){
BaseMouseListener.active = active;
}
protected abstract void doPerformAction(ActionEvent e);
@Override
public final void actionPerformed(ActionEvent e){
if(active){
doPerformAction(e);
}
}
}
Your listeners would have to implement doPerformAction() instead of actionPerformed().
More info : How to temporarily disable event listeners in Swing?
I don't know which language u using or what is your code. In Jquery i generally use the following 2 methods code
M1: to unbind one event of another.
OR M2: You should add event.stopPropagation() at the end of this event call to stop propogation..
M1 code sample:
else if(e.type == 'click')
{
$(window).unbind('mousemove')
}
But really you should name the event so you only unbind the appropriate event listener.
Bind : $(window).bind('mousemove.dragging', function(){});
Unbind : $(window).unbind('mousemove.dragging', function(){});
M2 Code sample:
$("#rightSubMenu").mousemove(function(e){
// You code...
e.stopPropagation();
});
Extra Info
For more info just see following tags Disable mousemove on click How to stop mousemove() from executing when the element under the mouse moves?
Upvotes: 0