Search code examples
javaswingjtextfield

How to insert a 'String' in a 'JTextField' to a specific position?


I want to insert a String in a JTextField. Something like-

myTextField.insert(text, position);

How can it be done? Is there any simple way to do this?


Solution

  • You may do this-

    jTextField.setText("This  swing.");    
    jTextField.getDocument().insertString(5, "is", null);
    

    And jTextField will show-

    This is swing.