public static <T extends JTextComponent> T addDefaultEditorActions(final T textComponent) {
    final UndoManager undoManager = new UndoManager();
    textComponent.getDocument().addUndoableEditListener(undoManager);

    final EditorActions actions = new EditorActions(undoManager);
    textComponent.setComponentPopupMenu(new DefaultEditorPopupMenu(actions));
    actions.getDelete().setEnabled(false);

    textComponent.addCaretListener(
        e -> {
          final boolean haveSelection = e.getDot() != e.getMark();
          final boolean haveClipboardData =
              Toolkit.getDefaultToolkit()
                  .getSystemClipboard()
                  .isDataFlavorAvailable(DataFlavor.stringFlavor);
          actions.getUndo().setEnabled(undoManager.canUndo());
          actions.getRedo().setEnabled(undoManager.canRedo());
          actions.getCut().setEnabled(haveSelection);
          actions.getCopy().setEnabled(haveSelection);
          actions.getPaste().setEnabled(haveClipboardData);
          actions.getDelete().setEnabled(haveSelection);
          actions.getSelectAll().setEnabled(textComponent.getText().length() > 0);
        });
    textComponent.setSelectionStart(0); // Fires the caret listener
    return textComponent;
  }
Adds default editor actions to Swing text fields.
java/ui/src/main/java/com/google/security/zynamics/bindiff/gui/components/TextComponentUtils.java