AWTEvents get lost in EventQueue (using postEvent)

Hello,

I'm posting custom event objects (derived from MouseEvent) to the SystemEventQueue, and sometimes, one of them seems to be "overwritten" by the next one.

More specificly, there's one part (thread) of the programme generating input and posting the events, and another listening for these events, and displaying them. Sometimes, thread scheduling works so unfavourable that two or more events are posted before the previous one/s have been dispatched. In this case, only the last event is dispatched. I put println() everywhere and added counting to all relevant classes, so that I could see what happens when, etc.

Something is definitely going wrong, but there seems to be no obvious issue with the code. Posting the code would only cause confusion. Maybe the console log shows the problem more clear; note that as a proof, I'm using the following code to be sure I didn't miss anything:

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener(){

publicvoid eventDispatched(AWTEvent event){

if (eventinstanceof MultitouchEvent){

System.out.println("=== event dispatched === " + event);

MultitouchEvent mte = (MultitouchEvent) event;

System.out.println("=== " +

mte.getSingleTouchEvents().get(0).count +" === ");

}

}

}, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);

This prints a message for every relevant event. The count member in the second println() can be understood as the frame number. Every frame generates one event, so the received event should always have the same number as the frame currently processed (i.e., it should be synchronous). Even if the event generation "overtakes" the event consumption, posted events should still be synchronously consumed, or in other words, the event from frame 23 should be received by the listener when it is invoked for the 23rd time.This may still sound very confusing, but hopefully it gets more clear looking at the console log:

[0] (MTI) posting event ...event.MultitouchEvent[MOUSE_PRESSED,...] on ... <-- first event gets posted

consumer: input done

=== event dispatched === ...event.MultitouchEvent[MOUSE_PRESSED,...] on ...

=== 0 ===

[0] input [0](MTDisplay) <-- event is being processed by the listener

[0] processing EVENT: pathID = 0 MTI-Count = 0 <-- here you can see everything still being synchronous:

0th step, event originates from the same step (MTI-Count)

[0](MTDisplay) +++ label: 0javax.swing.JLabel[...]

[1] PathTracer: START [1](PathTracer) <-- second frame begins; input is generated

[1]PathTracer: CREATE path 1

[1] PathTracer: END

consumer: input

[1] MTI: processing (ST)Events [1](MTI)

[2] PathTracer: START [2](PathTracer) <-- third frame begins before second frame's input has been processed

[2]PathTracer: CREATE path 2

[2] PathTracer: END

[1] STE: DRAGGED (908, 395) 0

[1] STE: PRESSED (77, 270) 1

[1] (MTI) posting event ...event.MultitouchEvent[MOUSE_DRAGGED,...] on ... <-- second event is posted

consumer: input done

[3] PathTracer: START [3](PathTracer) <-- fourth frame already begins...

[3]PathTracer: CREATE path 3

[3] PathTracer: END

consumer: input

[2] MTI: processing (ST)Events [2](MTI)

[2] STE: DRAGGED (908, 395) 0

[2] STE: DRAGGED (92, 265) 1

[2] STE: PRESSED (432, 78) 2

[2] (MTI) posting event ...event.MultitouchEvent[MOUSE_DRAGGED,...] on ... <-- third event is posted

consumer: input done

=== event dispatched === ...event.MultitouchEvent[MOUSE_DRAGGED,...] on ... <-- second event dispatched

consumer: input

[3] MTI: processing (ST)Events [3](MTI)

[3] STE: DRAGGED (908, 395) 0

[3] STE: DRAGGED (106, 275) 1

[3] STE: DRAGGED (432, 78) 2

[3] STE: PRESSED (447, 88) 3

[3] (MTI) posting event ...event.MultitouchEvent[MOUSE_DRAGGED,...] on ... <-- fourth event posted

consumer: input done

=== 1 === <-- (second event dispatched)

[1] input [1](MTDisplay) <-- second event received by listener

[1] processing EVENT: pathID = 0 MTI-Count = 1 <-- frame count of listener still matches with input's frame

[1](MTDisplay) <=> label: 0

[1] processing EVENT: pathID = 1 MTI-Count = 1

[1](MTDisplay) +++ label: 1javax.swing.JLabel[...]

=== event dispatched === ...event.MultitouchEvent[MOUSE_DRAGGED,...] on ... <-- fourth event dispatched -

but what about the third one?!?

=== 3 ===

[2] input [2](MTDisplay) <-- third listener invocation

[2] processing EVENT: pathID = 0 MTI-Count = 3 <-- but listener and event origin is now out of sync

[2](MTDisplay) <=> label: 0

[2] processing EVENT: pathID = 1 MTI-Count = 3

[2](MTDisplay) <=> label: 1

[2] processing EVENT: pathID = 2 MTI-Count = 3

[2](MTDisplay) <=> label: 2

... <-- third event (= number 2) does never occur...

As you can see, even the AWTEventListener does not receive the disappearing event, although it is sure that is has been posted to the event queue, and it is non-null.

Sorry for the confusing layout of the log, I hope you're not too confused.

I really appreciate your help.Thanks in advance,

Sascha

[5886 byte] By [ElkMonstera] at [2007-11-27 6:24:01]
# 1

D'oh! OK, nevermind...

One day lost, but problem finally resolved. Reading carefully helps.

http://java.sun.com/javase/6/docs/api/java/awt/Component.html#coalesceEvents(java.awt.AWTEvent,%20java.awt.AWTEvent)

"This implementation of coalesceEvents coalesces two event types: mouse move (and drag) events"

I was using MouseEvent.MOUSE_MOVED. Changed that and everything works like a charm.

ElkMonstera at 2007-7-12 17:42:25 > top of Java-index,Desktop,Core GUI APIs...