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.
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);
}
}
}
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);
}
}
}
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]