Java: debugging with SwingUtilities.invokeLater ()

I often use SwingUtilities.invokeLater(). However, in some cases, this makes debugging difficult: you do not see the stack trace of the code, which is called SwingUtilities.invokeLater()because this code has already completed its execution.

Are there any suggestions for setting up any context (for debugging purposes only) during the call SwingUtilities.invokeLater()so that you can figure out what caused the UI event?

+5
source share
6 answers

Most of the other answers here are good, but I want to add another sentence. If you call SwingUtilities.invokeLater very often, you probably do this unnecessarily for a while, especially if the only purpose of this call is to provide a Swing change in the event stream. Try it if necessary:

if (SwingUtilities.isEventDispatchThread()) {
  myRunnable.run();
} else {
  SwingUtilities.invokeLater(myRunnable);
}
+2
source

You can try redefining EventQueueand printing stacktrace for published events. Also in the example below, each event sent is assigned a unique number. When invokeLaterwill be called from another invokeLater, then the text postEvent 9 from 7will be printed in the magazine

        // Place this code somewhere in the main class to override queue
        EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
        eventQueue.push(new MyEventQueue());

MyEventQueue :

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.event.InvocationEvent;
import java.util.WeakHashMap;

public class MyEventQueue extends EventQueue {

    int currentNumber = 0;
    WeakHashMap<AWTEvent,Integer> eventIdMap = new WeakHashMap<AWTEvent,Integer>();
    AWTEvent currentEvent = null;

    protected void dispatchEvent(AWTEvent event) {
        if (event instanceof InvocationEvent) {
            currentEvent = event;
        }
        super.dispatchEvent(event);
        currentEvent = null;
    }

    public void postEvent(AWTEvent event) {
        if (event instanceof InvocationEvent) {
            currentNumber = currentNumber + 1;
            eventIdMap.put(event, currentNumber);
            System.out.println("postEvent " + currentNumber + " " +
                    (currentEvent != null ? "from " + eventIdMap.get(currentEvent) : "") );
            for(StackTraceElement element : new RuntimeException().getStackTrace()) {
                System.out.println("\t" + element);
            }
        }
        super.postEvent(event);
    }
}
+2

, , ... : .

runnable (, ). ,

 public static void myInvokeLater(final Runnable runnable) {
   long ts = System.currentTimeMillis();
   Log.info("call to invoke later, context :" + ts);
   Runnable r = new Runnable() {
            public void run() {
                Log.info("start runnable of invokeLater with context :" + ts);
                runnable.run();
            }
        };
   SwingUtilities.invokeLater(r);
}
+1

"" swingUtilites # invokeLater , , "localUtilities", , , - ( , , ).

+1

invokeLater, .

invokeLater . , EventQueue.invokeLater invokeLater isDispatchThread.

, , , invokeLater isDispatchThread, .

+1

Runnable invokeLater()?

, , , (: TableUpdateFromQuery). , . " ", .

+1

All Articles