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!!
[541 byte] By [
mmarseil] at [2007-11-15 20:12:19]

# 1 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 ???
poochi at 2007-11-10 2:53:47 >

# 2 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());
}
}
at 2007-11-10 2:54:41 >

# 3 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 ?
poochi at 2007-11-10 2:55:40 >

# 4 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.
at 2007-11-10 2:56:46 >

# 5 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.
at 2007-11-10 2:57:45 >
