TextField.setSelection without clicking

If you want to select a certain part of a textfield you use TextField.setSelection. This will only work however if you put it inside a click handler of the textfield.

Why? Because when you click on your textfield it receives focus, which is necessary for setSelection to work.

Solution? First assign focus to the textfield manually and then call setSelection:

stage.focus = tf;
tf.setSelection(0, 5);

1 Response to “TextField.setSelection without clicking”

  1. 1 AlexG

    Nice feature. I didnt think about setting the focus to select the text.

Leave a Reply