Modifying Text in a JTextComponent: Replace the first 3 characters with some text
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
public class Main {
public static void main(String[] argv) throws Exception {
JTextComponent textComp = new JTextField("Initial Text");
Document doc = textComp.getDocument();
// Replace the first 3 characters with some text
int pos = 0;
int len = 3;
doc.remove(pos, len);
doc.insertString(pos, "new text", null);
}
}
Related examples in the same category