23

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:

  • the implementation will be a simple wrapper around the normal Eventqueue, doing some logging.

  • the problem I'd like to debug is a TableCellEditor, which works fine in a little demo app, but when put in the real application, stopCellEditing gets called immediately, due to some event. I'd like to get access to the event in order to find out, where it is comming from.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Would [registering an AWTEventListener](http://docs.oracle.com/javase/6/docs/api/java/awt/Toolkit.html#addAWTEventListener%28java.awt.event.AWTEventListener,%20long%29) would be a simpler solution? – prunge Jan 23 '12 at 00:03
  • @andrew-barber How is this question too broad? It has two very nice concise answers, for the different java versions – Jens Schauder Mar 25 '16 at 09:28

3 Answers3

26

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);
        }
    }
}
rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
  • I could be wrong, but I think the problem isn't writing the `EventQueue` subclass, but rather with how to get AWT/Swing to use it instead of `EventQueue`. – Powerlord Jul 01 '10 at 14:02
  • 3
    But that's exactly what push seems to do. From the javadoc: push(EventQueue newEventQueue) Replaces the existing EventQueue with the specified one. – Jens Schauder Jul 01 '10 at 14:07
16

Be cautious with java 1.7. There's a bug. The solution posted by rancidfishbreath is perfect with java 1.6 but results in a Swing application that never exit with java 1.7. Under JDK 1.7, you have to install the new EvenQueue in the Event Dispatch thread ... and outside it in JDK 1.6 ... Write once, run everywhere ;-)

Here is an universal solution ... hope, 1.8 will not change it ;-)

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;

public class QueueTest {
    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        if (!isJava7Like()) setQueue();

        EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                if (QueueTest.isJava7Like()) setQueue();
                System.out.println("Run");
            }
        });
    }

    private static void setQueue() {
        EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
        eventQueue.push(new MyEventQueue());
    }

    private static boolean isJava7Like() {
        return Float.parseFloat(System.getProperty("java.specification.version")) > 1.6;
    }

    private static class MyEventQueue extends EventQueue {
        public void postEvent(AWTEvent theEvent) {
            System.out.println("Event Posted");
            super.postEvent(theEvent);
        }
    }
}
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
Jean-Marc Astesana
  • 1,242
  • 3
  • 16
  • 24
  • Interesting - though I notice that key events and mouse events (the ones I am interested in ) do not get sent in to "post" but are forwarded from the system event queue to "postEventPrivate()" which can not be overridden even with full access. :| – peterk Apr 15 '13 at 16:38
1

This is fine. Extending EventQueue will give you a handle on all AWTEvents.

How will you get a handle on all the Events. List of events is as below.

[AWTEvent, BeanContextEvent, CaretEvent, ChangeEvent, ConnectionEvent, DragGestureEvent, DragSourceEvent, DropTargetEvent, FlavorEvent, HandshakeCompletedEvent, HyperlinkEvent, LineEvent, ListDataEvent, ListSelectionEvent, MenuEvent, NamingEvent, NamingExceptionEvent, NodeChangeEvent, Notification, PopupMenuEvent, PreferenceChangeEvent, PrintEvent, PropertyChangeEvent, RowSetEvent, RowSorterEvent, SSLSessionBindingEvent, StatementEvent, TableColumnModelEvent, TableModelEvent, TreeExpansionEvent, TreeModelEvent, TreeSelectionEvent, UndoableEditEvent, UnsolicitedNotificationEvent]