JComponent (@NotNull JComponent parent) { if (myParent == null) { parent.addAncestorListener(new AncestorListenerAdapter() { @Override public void ancestorRemoved(AncestorEvent event) { if (myService != null) { myService.dispose(); } } }); } myParent = parent; return super.getKeyComponent(parent); }