Here you can find the source of replaceSelectionAndSelect(JTextComponent component, String text)
public static void replaceSelectionAndSelect(JTextComponent component, String text)
//package com.java2s; //License from project: Apache License import javax.swing.text.JTextComponent; public class Main { public static void replaceSelectionAndSelect(JTextComponent component, String text) {//w w w . j av a 2s .co m int position = Math.min(component.getSelectionStart(), component.getSelectionEnd()); component.replaceSelection(text); component.setCaretPosition(position); component.moveCaretPosition(position + text.length()); } }