How to replace the AWT EventQueue with own implementation [closed]
Asked Answered
B

3

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.

Bridlewise answered 1/7, 2010 at 13:31 Comment(2)
Would registering an AWTEventListener would be a simpler solution?Konstance
@andrew-barber How is this question too broad? It has two very nice concise answers, for the different java versionsBridlewise
D
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);
        }
    }
}
Disparate answered 1/7, 2010 at 13:50 Comment(2)
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.Corny
But that's exactly what push seems to do. From the javadoc: push(EventQueue newEventQueue) Replaces the existing EventQueue with the specified one.Bridlewise
L
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);
        }
    }
}
Lil answered 22/1, 2012 at 22:53 Comment(1)
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. :|Consummate
C
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]

Creamer answered 29/10, 2014 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.