void (@NotNull Component component, @NotNull Runnable runnable) { assert component.isShowing(); if (component.isFocusOwner()) { runnable.run(); } else { Disposable disposable = Disposer.newDisposable(); FocusListener focusListener = new FocusAdapter() { @Override public void focusGained(FocusEvent e) { Disposer.dispose(disposable); runnable.run(); } }; HierarchyListener hierarchyListener = e -> { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && !component.isShowing()) { Disposer.dispose(disposable); } }; component.addFocusListener(focusListener); component.addHierarchyListener(hierarchyListener); Disposer.register(disposable, () -> { component.removeFocusListener(focusListener); component.removeHierarchyListener(hierarchyListener); }); } }