Hi there,
I see a discrepancy in the implementation of EventQueue's postEvent()
vs. getNextEvent() (or peekEvent() for that matter).
Suppose we have a stack of 2 event queues, eq1 and eq2. eq1 is the
system event queue, i.e. the bottom of the stack. eq2 is pushed on top
of it.
Now, if I call Toolkit.getSystemEventQueue() what I get is eq1 and when
I call postEvent(), it goes to the top of the stack, i.e. eq2, and posts
the event in that queue.
However, peekEvent() and getNextEvent() don't do the same. When I call
getNextEvent() or peekEvent() on the eq1, it does never see the event I
just posted before.
I find this a bit irritating. When I post an event on the sys EQ, I
would expect the event to appear in the same EQ (from the point of view
of my code, I only ever see this system EQ). The fact that another EQ is
pushed on top of this changes this behavior. I would like to understand
if there is a reason behind this implementation discrepancy, and if yes,
what it is.
Find attached an example that exposes the problem. When you change the
queue in the inner while loop to query eq2 directly, it works, otherwise
the even never shows up in that loop. (Yes I know there's also the EDT,
and I should probably change the example so that the inner loop runs in
the EDT, but it would be the same).
Cheers, Roman
import java.awt.*;
import java.awt.event.*;
public class EQTest {
public static void main(String[] args) throws Exception {
final EventQueue q1 = Toolkit.getDefaultToolkit().getSystemEventQueue();
final EventQueue q2 = new EventQueue();
q1.push(q2);
Object source = new String("My event");
Thread t = new Thread() {
public void run() {
while (true) {
try {
System.err.println("got event: " + q1.getNextEvent());
} catch (InterruptedException ex) {
}
}
}};
t.start();
q1.postEvent(new InvocationEvent(source, new Runnable() {
public void run() {}}));
System.err.println("posted event to EQ1");
}
}