The call later is excellent, even if you are on the EDT, however, it certainly changes the time of events, so you must be sure that you were not dependent on the code sequence here when you were on the EDT. In this case, a simple way to avoid forgetting else is to wrap the call in the utility:
public static void invokeInDispatchThreadIfNeeded(Runnable runnable) { if (EventQueue.isDispatchThread()) { runnable.run(); } else { SwingUtilities.invokeLater(runnable); } }
This way you will never forget else .
Also, overall in your idom, repeating code x is a very bad idea, as you can fix or improve this code later, and you will only do this in one place, leaving the error in another.
source share