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

Posted on 2008-11-19
Last Modified: 2012-06-27

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!
Question by:tmitch68
    1 Comment
    LVL 12

    Accepted Solution

    Use JTextComponent.setSelectionStart(int) and JTextComponent.setSelectionEnd(int). Read details here:

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    copyEvens challenge 6 47
    Basics of Drool Rule Engine with few samples 2 31
    java continue statement 10 53
    network + 7 59
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now