In Java, which event is continuously * fired when the mouse button is clicked?

I would like to know if an event exists in Java continuously when the mouse button is held down (pressed), even if the mouse does not move. I could not find it in typical MouseListeners:

  • MouseDragged is launched only if the user moves the mouse while holding the button down
  • MousePressed only starts once when the button is pressed

And about that. Any idea how to make such an event?

Greetings

Jy

+4
source share
5 answers

James Goodwin code will not work. mousePressed and mouseReleased both start from the GUI thread, so locking in mousePressed will prevent mouseReleased from starting because the loop will last forever.

If you already have a separate thread for processing, use mousePressed to point to the thread that the event should start, and mouseReleased to stop.

If you don't have a separate thread and don’t want the hassle, a timer is probably the easiest way. javadoc on Timer .

In particular, you must create a TimerTask that does what you want to do several times, and queue it using Timer.schedule:

Timer timer = new Timer(); TimerTask task = new MyTimerTask(); private class MyTimerTask extends TimerTask { public void run() { // your code here } } public void mousePressed(MouseEvent e) { timer.scheduleAtFixedRate(task, 0, 1000); // Time is in milliseconds // The second parameter is delay before the first run // The third is how often to run it } public void mouseReleased(MouseEvent e) { task.cancel(); // Will not stop execution of task.run() if it is midway // But will guarantee that after this call it runs no more than one more time } 

I am sure that this is the easiest way, since it is not related to communication between threads.

Oh, and, as Peter said, you will need to add the code for the user account that will run on your application and somewhere else.

+5
source

There is an obvious reason this event is not available in MouseListener: it is going to spam you with such events that everything slows down to a halt. Do you want to receive this event every second, every ms or even more often? If you need it, you will have to do it yourself.

To test this process, you certainly need mousePressed and mouseReleased to determine if the button is currently held. Then you need to run some kind of loop that generates the appropriate events that you would like to have.

You can also work with a survey, i.e. extend your MouseListener class so that it can tell you whether the button remains pressed, and if you need these events, you can actively poll the button. It depends on how you want to use these events, which approach is more suitable.

+8
source

If you need to do something while the mouse button is not working, just run it when you detect the mousePressed event, and then continue to do this until you find the mouseReleased event. Then you do not need to constantly shoot at your event. eg.

 public void mousePressed(MouseEvent e) { someCondition = true; while(someCondition) { //do something } } public void mouseReleased(MouseEvent e) { someCondition = false; } 

EDIT: As others have said, the code must be separated from the event stream, otherwise the mouseReleased call will be blocked so that the loop does not end.

+7
source

No such event

You can create your own by starting the timer in the mousedown method and ending with the same timer in mousereleased

You will also need a few crashing failures to make sure the timer stops when you make a mousedown movemove to another component or even to other frame elements or not java gui.

+1
source

I think you will find the answer is no, there is no such event. Not only for Java, but for any graphical interface. It will serve no purpose other than linking the queue of events.

You will need to create your own by capturing the mouse event and then firing the timer event at regular intervals until the mouse up event.

0
source

All Articles