[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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!
1 Solution
Use JTextComponent.setSelectionStart(int) and JTextComponent.setSelectionEnd(int). Read details here:


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now