Click to See Complete Forum and Search --> : Set focus to the last character of a textbox
mmarseil
May 8th, 2000, 04:09 PM
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!!
poochi
May 8th, 2000, 05:10 PM
you can use "setCaretPosition()" method for this..
textField.setText( strText );
textField.setCaretPosition( strText.length()); // length() - 1 ?????
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());
}
}
poochi
May 9th, 2000, 05:50 PM
> 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 ?
"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.
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.