Is it possible to set a JTextField so that - when you tab to it - several of the characters are selected?


I have a Java app that has JTextFields.

What I want to do is set a JTextField so that - when the user tabs to the JTextField - the last 9 characters in the text field are selected (ie:  if you tab to the JTextField, the last 9 characters will be deleted if the user hits the delete key).

I notice that the JTextField already does this automatically - if you manually selected the last 9 characters using the mouse, and then focus on another field, and tab back to the JTextField, the JTextField "remembers" that the last 9 chars were selected.

What I need to do is be able to set this in the code when the form first comes up - without having to set it manually.

Thanks in Advance!
Who is Participating?
marklorenzConnect With a Mentor Commented:
Use JTextComponent.setSelectionStart(int) and JTextComponent.setSelectionEnd(int). Read details here:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.