auto-repeat MouseEvent
Hi,
i'm looking for a way of 'auto-repeating' mouse events. that is, i have a button, which has a mouse listener. if the button is pressed or clicked, i get the appr. event.
what i want is, that as long as the mouse is pressed, i get repeated events.
anybody out there has an idea how to achieve this? i do not insist on using a MouseListener so i'm open to suggestions.
thomas
[419 byte] By [
kloeber] at [2007-9-26 4:37:23]

at mopusePressed you start some loop somewhere at mousereleased you stop it.
It propably would be wise to make it in its own thread as it might block the eventhandlethread.
If you use the setup
while(mp)
{
//do something
}
and set mp false when mousereleased i think you need to declare mp synchronized (or something similiar) to make it update.
never have tried or closely looked at thread stoping methods but that would be a good place to look.
arcosh at 2007-6-29 17:56:11 >

Use addMouseMotionListener() instead of addMouseListener() and provide the code into the mouseDragged() method.
hmmm, sounds a bit over the top to me 'coz you're right, your while(mp) {} has to run in a separate thread otherwise my output never gets updated, the mouseReleased never triggers, etc...
isn't there a simpler way? how does the scrollbar do it? anybody has an idea?
thomas
ps: acrosh's solution doesn't work in my case, because my alogorithm pops up JOptionPanes on occassions, after that i never get a mouseReleased hence my while loop goes on forever (neat to watch tho ;)
sorry, but this is no good. i'm not getting a mouseDragged event at all.thomas
Well, MouseMotionListener should be used to track mouse movement over a component. Maybe some default on AbstractButton disable this events, I haven't tried with a JButton.
Here is an example on how to use this listener:
http://java.sun.com/docs/books/tutorial/post1.0/ui/mousemotionlistener.html
my reply was not quite correct. of course i do get the mouseDragged event but only if i press the button and then wiggle the mouse on top of it. but...would you want to use such a GUI ;)?thomas
1) On MouseListener.mousePressed() start a javax.swing.Timer (ScrollBar uses a 60 msec delay). The ActionListener you attach to this should perform the processing you require for each repetition.2) On MouseListener.mouseReleased stop the Timer.
wathed at 2007-6-29 17:56:11 >

What about adding a MouseMotionListener to the GlassPane?You always can use the getComponentAt() from the ContentPane to locate your components.
LLuis,
> What about adding a MouseMotionListener to the
> GlassPane?
> You always can use the getComponentAt() from the
> ContentPane to locate your components.
a MouseMotionListener is out of the question, because it requires the mouse to move to fire its events!
thomas
its ver much a hack but what about installing a timer that regulary checks some conditions that determine if the action should be repeated. (either you do things at every timermoment or you use the timer to start stop the loop)
arcosh at 2007-6-29 17:56:11 >

I was really understanding that you want to do some kind of drag/drop within a frame. Finally I see want you mean, sorry.
If you create a Timer with your task in the ActionListener of this timer you have control on the delay among method calls and when they should start (on mouse pressed) and stop (on mouse released)
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
... your task
}
});
timer.setRepeats(true);
// mouse pressed
if(!timer.isRunning()) timer.start();
// mouse released
if(timer.isRunning()) timer.stop();
i can use Thread or Timer, doesn't make much difference (dunno about the performance tho).
everything works fine, but...
here is another hitch: the functionality in question is attached to a generic widget which fires adjustement events if the user presses the button and things change. now, i have a listener which, when receiving a adjustment event, can pop up a JOptionPane, asking for some confirmation. after that has happened i can't seem to stop my loop properly.
i tried mouseReleased (never get it after the JOP) and mouseExited (i only get it once the mouse is back inside the dialog, till the my loop happily continues).
anymore ideas/suggestions?
thomas
Do i understand that correctly, while the user keeps the mousebutton down dialogs apeare that ask the user for confirnmation?
how does the user confirm with the mousebutton down all the time? Exclusivly with the keyboard?
If this is true this is an unusual setup.
What about making a start/stop button that starts and stops the loop. Or a component where the loop is activated iff the mouse is over the component.
Don't know what exactly you want to do but theese 2 seem easier to programm.
arcosh at 2007-6-29 17:56:11 >

> Do i understand that correctly, while the user keeps
> the mousebutton down dialogs apeare that ask the user
> for confirnmation?
not quite, see below
> how does the user confirm with the mousebutton down
> all the time? Exclusivly with the keyboard?
no, since JOptionPane is modal the user just presses the appr button
here are some more details:
i have a text field which contains a counter;
i have 2 BasicArrowButtons which allow the user to incr/decr the counter;
when the counter is changed it fires an adjustment event;
i have a listener which, at certain counter values, asks for user confirmation via a JOptionPane.
i would like the user to be able to keep one of the buttons pressed to automatically incr/decr the counter, while the button is down without having to continously press the mouse button (think of the strain and the wear on the mouse ;).
so start/stop options or an additional component don't seem appropriate
I see.
and the problem is IIRC that if a Dialog apears situation happens during keeping the button pressed you get no more mousereleased events. Is that correct?
If you don't have a dialog at every other step you could call the same mentod you call in mousereleased before or after you show such a dialog. the user will have to click again afterwards but as long as there are not too many dialogs it would not make such a difference.
An other option that could or could not fit in your design would be to add a horizontal scrollbar below your textfield and use this.
> and the problem is IIRC that if a Dialog apears
> situation happens during keeping the button pressed
> you get no more mousereleased events. Is that
> correct?
yup
> If you don't have a dialog at every other step you
> could call the same mentod you call in mousereleased
> before or after you show such a dialog. the user will
> have to click again afterwards but as long as there
> are not too many dialogs it would not make such a
> difference.
the problem is, that the object/method that opens the option pane is only an adjustment listener and the object/method that does the counter changes and fires the events has no knowledge when/if an option pane has popped up.
> An other option that could or could not fit in your
> design would be to add a horizontal scrollbar below
> your textfield and use this.
this was my 1st approach and it looked horrible, esp on NT, that's why i decided to use the ArrowButtons.
> the problem is, that the object/method that opens the
> option pane is only an adjustment listener and the
> object/method that does the counter changes and fires
> the events has no knowledge when/if an option pane has
> popped up.
>
can't you put enough references everywhere that you can access one from the other? (i know if you already have written a lot of code this is lots of work)
If it's an application and not an applet you also can works with static variables and methods.
> can't you put enough references everywhere that you
> can access one from the other? (i know if you already
> have written a lot of code this is lots of work)
i could, but its ugly and as you say a lot of work.
apart from that it's much against the design...
> If it's an application and not an applet you also can
> works with static variables and methods.
the static bit sounds reasonable, i might give it a shot...
thanx for all your suggestions
Would a JSlider not serve your purpose ?
> Would a JSlider not serve your purpose ?not really, it is not, IMHO, the appr input method to incr/decr a counter value
If I had this requirement I would arrange things slightly differently and I believe this would make the issue disappear. The problem stems from the fact that the adjustments are being listened to whilst the user is in the process of making an adjustment. That is to say you are currently considering each 'tick' of the counter as an adjustment. I would say that the user gesture of pressing the button, holding it down and then releasing it when the desired value is reached is a single adjustment. So the broad scheme would be:
1) User presses mouse - start Timer and copy current value.
2) Each time the Timer fires increment the copy value and display it in the text box.
3) User releases button - Stop Timer, set copy value as the real value thus causing an adjustment event to fire.
4) Listener get AdjustmentEvent and displays option pane if appropriate.
> 1) User presses mouse - start Timer and copy current
> value.
> 2) Each time the Timer fires increment the copy value
> and display it in the text box.
> 3) User releases button - Stop Timer, set copy value
> as the real value thus causing an adjustment event to
> fire.
> 4) Listener get AdjustmentEvent and displays option
> pane if appropriate.
BINGO, full marks (and $$) to Dave!
of course, but one tends to get a bit one-sighted when looking for a solution for a while.
this together with a timer (or a thread) does exactly what i want.
thanx for all the input.
thomas
I don't know what they are discussing about?just implement mousePressed(). If this method exists and the mousebutton is down events will be fired continously as long as the button is down.
> I don't know what they are discussing about?
> just implement mousePressed(). If this method exists
> and the mousebutton is down events will be fired
> continously as long as the button is down.
not with my Java, i get exactly 1 mousePressed event no matter how long i hold the mouse down (you can let go now, Fred... ;)! hence the discussion...
no reason to become harshly