CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2000
    Location
    San Diego, CA
    Posts
    4

    Set focus to the last character of a textbox

    Does anyone know how to set focus to the last character of a textbox in javascript or in a java applet. For example, when a page loads a value will be set in a textbox. Take the word "sample", for instance. I want to set focus to the textbox and the cursor should appear after the "e" in "sample". I've tried to populate that textbox and then set focus, hoping that the cursor would be at the end since there was already text in there, but it just put the cursor at the beginning of the word. Any help would be greatly appreciated!!


  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Set focus to the last character of a textbox


    you can use "setCaretPosition()" method for this..


    textField.setText( strText );
    textField.setCaretPosition( strText.length()); // length() - 1 ?????





  3. #3
    Guest

    Re: Set focus to the last character of a textbox

    I put a button to implement your code, and found everytime I pressed the button the cursor was gone. The caret position I got is ok. How can I move the cursor where the caret is. The following is part of my code.

    class listener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    tf.setCaretPosition(tf.getText().length() -1);
    System.out.println(tf.getCaretPosition());
    }
    }


  4. #4
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Set focus to the last character of a textbox


    > How can I move the cursor where the caret is

    You cannot programmatically move the Cursor from one place to another.

    BTW , what do you mean by "the cursor was gone" ? Didnt you get I-Beam Cursor ? I am using
    Swing Components. In Swing JTextField , I am getting I Beam Cursor. What happen if you move
    your mouse ?


  5. #5
    Guest

    Re: Set focus to the last character of a textbox

    "The cursor was gone.": I-Beam Cursor disappeared. I thought the I-Bean Cursor should stay where the caret position is. I am not sure if it is right.


  6. #6
    Guest

    Re: Set focus to the last character of a textbox

    Poochi, thank you for your reply. I found the problem. The reason for the disappearance is that the button got the focus. I have the textfield get focus, and everythink is ok.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured