Finding the cursor text position in JTextField

tadpole picture tadpole · Jun 13, 2012 · Viewed 10.8k times · Source

Is there a method to return the position of the character in the JTextField. What I mean by that is if I have a JTextField with some values in it. For example, the field contains value ABCDEFJ. The user decides to put the cursor right after the character 'C' to enter a new value. Is there a method to get position where he enters the new character. In this example, that would return a 3.

Answer

alaster picture alaster · Jun 13, 2012

JTextField.getCaretPosition()

JTextField.setCaretPosition(int pos)