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

杀马特。学长 韩版系。学妹 提交于 2019-11-26 03:25:25

问题


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.


回答1:


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



回答2:


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



回答3:


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]



来源:https://stackoverflow.com/questions/3158254/how-to-replace-the-awt-eventqueue-with-own-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!