void (int startOffset, int endOffset) { JTextComponent textComponent = getTextComponent(); if (textComponent.getCaretPosition() == startOffset) { // avoid moving caret (required for correct Ctrl-W operation) textComponent.setCaretPosition(endOffset); textComponent.moveCaretPosition(startOffset); } else { textComponent.setCaretPosition(startOffset); textComponent.moveCaretPosition(endOffset); } }