void (@NotNull DocumentEvent e) { Document document = e.getDocument(); if (document instanceof AbstractDocument) { StackTraceElement[] stackTrace = new Throwable().getStackTrace(); for (StackTraceElement element : stackTrace) { if (!element.getClassName().equals(JTextComponent.class.getName()) || !element.getMethodName().equals("setText")) continue; UndoableEditListener[] undoableEditListeners = ((AbstractDocument)document).getUndoableEditListeners(); for (final UndoableEditListener listener : undoableEditListeners) { if (listener instanceof UndoManager) { Runnable runnable = ((UndoManager)listener)::discardAllEdits; SwingUtilities.invokeLater(runnable); return; } } } } }