javaswingeventqueue

How to replace the AWT EventQueue with own implementation


In order to debug strange behavior in a Swing-application I'd like to replace the AWT EventQueue with my own implementation.

Is this possible? How?

Just in case you are interested:


Solution

  • EventQueue has a method called push() that will do exactly what you want. Here is a little demo:

    public class QueueTest {
        public static void main(String[] args) throws InterruptedException, InvocationTargetException {
            EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
            eventQueue.push(new MyEventQueue());
    
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    System.out.println("Run");
                }
            });
        }
    
        private static class MyEventQueue extends EventQueue {
            public void postEvent(AWTEvent theEvent) {
                System.out.println("Event Posted");
                super.postEvent(theEvent);
            }
        }
    }