void (@NotNull String text) { Document oldDocument = getEditorTextField().getDocument(); String oldText = oldDocument.getText(); for (DocumentListener listener : myDocumentListeners) { oldDocument.removeDocumentListener(listener); } Document document = myFactory.fun(text); getEditorTextField().setDocument(document); for (DocumentListener listener : myDocumentListeners) { document.addDocumentListener(listener); } WriteAction.run(() -> document.setText(text)); }