Jean-Yves
Jean-Yves

Reputation: 699

In Java, what event is *continuously* fired on mouse button down?

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

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

Cheers

jy

Upvotes: 5

Views: 17320

Answers (5)

James Goodwin
James Goodwin

Reputation: 7406

If you need to do something whilst the mouse button is down, just start it when you detect a mousePressed event and then continuously do that until you detect a mouseReleased event. Then you don't need to have your event continuously firing. e.g.

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 would need to be separated from the event thread otherwise the call to mouseReleased would get blocked preventing the loop from ending.

Upvotes: 8

ZoFreX
ZoFreX

Reputation: 8920

James Goodwin's code will not work. mousePressed and mouseReleased are both fired from the GUI thread, so blocking in mousePressed will prevent mouseReleased from ever being fired, meaning the loop will continue forever.

If you already have a seperate thread for processing then use mousePressed to indicate to that 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.

Specifically, you should create a TimerTask that does whatever it is you want to do multiple 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'm pretty sure this is the simplest way, as it doesn't involve faffing around with inter-thread communication.

Oh, and as Peter said, you will have to add code to account for the user mousing down on your app and mousing up somewhere else.

Upvotes: 6

Dave Kirby
Dave Kirby

Reputation: 26592

I think you'll find the answer is no, there is no such event. Not just for Java, but for any GUI framework. It would serve no purpose other than to tie up the event queue.

You would need to create your own by trapping the mouse down event then having a timer fire events at regular intervals until the mouse up event.

Upvotes: 0

Peter
Peter

Reputation: 5798

There is no such event

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

You will also need a few fail saves to make sure the timer stops when you do mousedown the movemove onto another component or even onto other frame or non java gui parts

Upvotes: 1

Frank
Frank

Reputation: 10571

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

For stearing this process you of course need mousePressed and mouseReleased to determine whether a button is currently held down. Then you need to run some kind of loop that generates the corresponding events you'd like to have.

You might also want to work via polling, i.e. extend your MouseListener class such that it can tell you whether a button is still held down, and whereever you need those events you can actively poll for the button. It depends on how you want to use these events, which approach is more suitable.

Upvotes: 9

Related Questions