Java tutorial
//package com.java2s; /* * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ import java.util.Set; import java.util.HashSet; import com.sun.javafx.tk.Toolkit; import javax.swing.*; public class Main { private static final Set<Object> eventLoopKeys = new HashSet<>(); /** * The runnable is responsible for leaving the nested event loop. */ static void runOnEDTAndWait(Object nestedLoopKey, Runnable r) { Toolkit.getToolkit().checkFxUserThread(); if (SwingUtilities.isEventDispatchThread()) { r.run(); } else { eventLoopKeys.add(nestedLoopKey); SwingUtilities.invokeLater(r); Toolkit.getToolkit().enterNestedEventLoop(nestedLoopKey); } } }