To bring focus to a TextInput control in flex we can use a method called setFocus().
ex: textinputreference.setFocus();
This will bring the focus to the TextInput of id “textinputreference”.
check the below example.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml“
layout=”absolute” creationComplete=”fnInit()”>
<mx:Script>
<![CDATA[
private function fnInit():void
{
txtInp.setFocus();
}
]]>
</mx:Script>
<mx:TextInput id=”txtInp” x=”381″ y=”206″/>
</mx:Application>
Updated ::
For the above code to work we need to send the focus of the Flex App to the Flash Player with some Java Script methods,by default it does not send the focus untill unless you click on the App.
Code:
//The below code go in to the <script /> tag in your HTML file.
function setFlexAppFocus()
{
getFlexAppMovieObject(‘TextInputFocus’).focus();//TextInputFocus is id in <object> tag in you HTML file.
}
function getFlexAppMovieObject(movieName)
{
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf(“Microsoft Internet”)==-1)
{
if (document.embeds && document.embeds[movieName])
{
return document.embeds[movieName];
}
}
else
{
return document.getElementById(movieName);
}
}
//The below line is your <body> tag in your HTML file.
<body scroll=”no” onload=”setFlexAppFocus()”>
Enjoy the post.


November 12, 2008 at 3:14 pm |
This does not put the cursor into the field, it only sets the focus into the field. does anyone know how to load an application and place a cursor into a specific text field on creationComplete?
January 7, 2009 at 3:09 pm |
I’m having the same issue. the focus is ‘drawn around a text input filed but you cannot write into it. (I’m also havign a problem where you cannot click on teh field and hten write into it so there may be some other problem at work here.)
however, i am also having trouble setting focus to teh first of a group of radio buttons or checkboxes.
January 9, 2009 at 4:22 am |
Hi,
To resolve this problem we got to get the focus.Use this line addedToStage=”stage.focus=null;” , it will bring the focus to the current stage and sets the focus.Like if the Textfield is in TitleWindow so add this line to the TitleWindow.
regards,
kumar.
March 3, 2009 at 9:21 am |
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
March 9, 2009 at 6:25 am |
I’m still struggling with the focus issue.
Trying all your suggestions still not working. Also using Firefox. Here is my code. . .
Can you help?
March 9, 2009 at 8:23 am |
Hi,
Can you mail me the code sample.
regards,
kumar.
June 23, 2009 at 12:27 pm |
Hi
Please let me know the solution!
I tried as u said, its not working
Thanks
Aruna
June 30, 2009 at 8:19 am |
Howzit all
I came across this error as well
after some searching found out that it is logged
as a bug in Adobe but they have a work around:
http://bugs.adobe.com/jira/browse/SDK-15319
use the callLater() function
with some tweaking i got this method to work for me
I use it on a textInput field’s focusOut, if it fails a RegExp test, i set some vars, and on a focusIn method checks if the previous field failed, then the method sets focus back to the previous textInput as well as show an errorString. I made the focusIn and focusOut methods dynamic, so i use it over on all my required fields
hopefully this helps
here’s my code example:
//mValidation is a Boolean
//lPreviousItem is a String
private function checkFocusInAmount(field:Event)
:void {
if(!mValidationError) {
var lText:String = field.currentTarget.text;
if(Number(lText) == 0) {
field.currentTarget.text = “”;
}
}
else {
callLater(
field.currentTarget.focusManager.setFocus
[this[mPreviousItem]]);
}
}
private function checkFocusOutAmount(field:Event)
:void {
var lText:String = field.currentTarget.text;
var lTest:RegExp = /^[0-9]+([\.][0-9]{1,2})?$/;
if(lText == “”) {
field.currentTarget.text = “0.00″;
}
else {
if(lTest.test(lText)) {
field.currentTarget.text =
Number(lText).toFixed(2);
field.currentTarget.errorString = ”;
mValidationError = false;
}
else {
field.currentTarget.errorString =
‘Amount is in the incorrect format.
\nOnly an Amount with up to 2 decimal
places allowed.\nAmount format: #.##’;
mValidationError = true;
mPreviousItem = field.currentTarget.id;
}
}
}
June 30, 2009 at 8:21 am |
sorry my code doesn’t look very neat, should still be readable
December 11, 2009 at 6:52 am |
This does not work: it only highlights around the textbox, but you can’t type in it as it doesn’t have a cursor in it.
Adding that stage.focus trick made things worse: text box is not even highlighted!
December 11, 2009 at 8:05 am |
I have just updated the post, and its completely resolved and working fine, comment me for queries.
regards,
kumar.
December 22, 2009 at 7:17 pm |
Kumar,
Where is the updated code that you have put.
December 22, 2009 at 7:26 pm |
Kumar,
I guess the updated code isn’t there on the post
December 23, 2009 at 6:46 am |
Hi Krishna,
Its on the post itself,after the Updated:: line with red text is the Update to the Post.
regards,
kumar.