How to replace AWQ EventQueue with your own implementation

To debug strange behavior in a Swing application, I would like to replace AWT EventQueue with my own implementation.

Is it possible? How?

Just in case, you are interested in:

  • the implementation will be a simple wrapper around a regular Eventqueue, performing some logging.

  • The problem I would like to debug is TableCellEditor, which works fine in a small demo application, but when it is put into a real application, stopCellEditing is called immediately due to some kind of event. I would like to access the event to find out where it is coming from.

+21
java swing
Jul 01 '10 at
source share
3 answers

EventQueue has a push () method that will do exactly what you want. Here is a small 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); } } } 
+23
Jul 01 '10 at 13:50
source share

Be careful with java 1.7. There is a bug . The solution hosted by rancidfishbreath is excellent with java 1.6, but leads to a Swing application that never comes out with java 1.7. In JDK 1.7, you need to install a new EvenQueue in the Event Dispatch stream ... and outside of it in JDK 1.6 ... Write once, run everywhere; -)

Here is a universal solution ... I 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); } } } 
+14
Jan 22 2018-12-22T00:
source share

This is normal. The EventQueue extension will give you access to all AWTEvents.

How do you get an appeal to all events. The list of events is as follows.

[AWTEvent, BeanContextEvent, CaretEvent, ChangeEvent, ConnectionEvent, DragGestureEvent, DragSourceEvent, DropTargetEvent, FlavorEvent, HandshakeCompletedEvent, HyperlinkEvent, LineEvent, ListDataEvent, ListSelectionEventE ,EndEChangePreview, MenuEvent, NamingEvent, NoamingChange , SSLSessionBindingEvent, StatementEvent, TableColumnModelEvent, TableModelEvent, TreeExpansionEvent, TreeModelEvent, TreeSelectionEvent, UndoableEditEvent, UnsolicitedNotificationEvent]

+1
Oct 29 '14 at 13:01
source share



All Articles