Link to home
Start Free TrialLog in
Avatar of naveenm_006
naveenm_006Flag for India

asked on

How to focus a textInput Field by default in Flash as3?

How to focus a textInput Field by default in Flash as3?

Regards,
Naveen.
Avatar of deepanjandas
deepanjandas
Flag of India image

stage.focus = textField;//textField is the name of the textField instance.

Warm Regards
Deepanjan Das
Avatar of naveenm_006

ASKER

But i am not getting the desired o/p that is not focusing on the textInputField.
sampark-bottom.fla
Try this:
sampark-bottom.fla

Warm Regards
Deepanjan Das
Thanks for your response. After running your fla file i am getting desired output in swf file.But when i include this generated swf file in flex swfloader i am not getting the desired output ie., not focusing on textInputField by default.What might be the problem.Anyway i am sharing my sample snippet for your reference.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="gray" height="100%" verticalScrollPolicy="auto" horizontalScrollPolicy="auto">

    <mx:Script>
        <![CDATA[
            
              import mx.managers.SystemManager;
              import mx.controls.Alert;
              [Bindable] 
		       public var loadedSM:*;
                       
                       // Initialize variables with information from
                       // the loaded application.
                private function initNestedAppProps() : void {
                               loadedSM = loader2.content;
                               loadedSM.addEventListener( MouseEvent.CLICK, doLogin, false, 0, true );
                       }
		        public function doLogin( event:MouseEvent ) : void 
                       {                                
                               //"userTextFieldInstanceName" is the username text field instance name
                               if(event.target.name == "loginButtonInstanceName"){
                               var username:String = loadedSM.userTextFieldInstanceName.text;
                               var password:String = loadedSM.passTextFieldInstanceName.text;
                               /* trace(username);
                               trace(password); */
                               if(username.length<=0 || password.length<=0)
                               {
                               	Alert.show('username/password can not be empty');
                               }
                                	else{
                               	Alert.show('validating...');
                               	
                               	
                               }      
  }
                       }
               ]]>
       </mx:Script>        
       <mx:SWFLoader id="loader1" source="assests/sampark_HR.swf" width="100%" height="100%"/>
       <mx:SWFLoader id="loader2" source="assests/sampark-bottom.swf" width="100%" height="100%" complete="initNestedAppProps()" x="0" y="450.95"/>        
             
</mx:Application>

Open in new window

sampark-HR.swf
sampark-bottom.swf
Try this:
 
private function initNestedAppProps() : void {
       loadedSM = loader2.content;
       loadedSM.addEventListener( MouseEvent.CLICK, doLogin, false, 0, true );
       stage.focus = loadedSM.userTextFieldInstanceName;
}

Open in new window


Warm Regards
Deepanjan Das
I am getting below error after adding  stage.focus = loadedSM.userTextFieldInstanceName

TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at main/initNestedAppProps()[D:\naveen.kumar\Documents\Flex Builder 3\hrhelpdesk\src\main.mxml:18]
      at main/__loader2_complete()[D:\naveen.kumar\Documents\Flex Builder 3\hrhelpdesk\src\main.mxml:73]
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9440]
      at mx.controls::SWFLoader/http://www.adobe.com/2006/flex/mx/internal::contentLoaderInfo_completeEventHandler()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\controls\SWFLoader.as:2064]
I am getting the following error after adding  stage.focus = loadedSM.userTextFieldInstanceName in initNestedAppProps() method

TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at main/initNestedAppProps()[D:\naveen.kumar\Documents\Flex Builder 3\hrhelpdesk\src\main.mxml:18]
      at main/__loader2_complete()[D:\naveen.kumar\Documents\Flex Builder 3\hrhelpdesk\src\main.mxml:73]
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9440]
      at mx.controls::SWFLoader/http://www.adobe.com/2006/flex/mx/internal::contentLoaderInfo_completeEventHandler()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\controls\SWFLoader.as:2064]
This suggests that the value is null for the instance. Try this:
private function initNestedAppProps() : void {
       loadedSM = loader2.content;
       loadedSM.addEventListener( MouseEvent.CLICK, doLogin, false, 0, true );
       stage.focus = loadedSM["userTextFieldInstanceName"];
}

Open in new window


Warm Regards
Deepanjan Das
This time i am getting the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at main/initNestedAppProps()[D:\naveen.kumar\Documents\Flex Builder 3\hrhelpdesk\src\main.mxml:18]
      at main/__loader2_complete()[D:\naveen.kumar\Documents\Flex Builder 3\hrhelpdesk\src\main.mxml:75]
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9440]
      at mx.controls::SWFLoader/http://www.adobe.com/2006/flex/mx/internal::contentLoaderInfo_completeEventHandler()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\controls\SWFLoader.as:2064]
Can you please pass be the mxml file and sampark-bottom.fla

Warm Regards
Deepanjan Das
Hi plz find the attached files.Also check in layer 16 frame15 there is actionscript code written. Whether this might cause any problem.
main.txt
sampark-bottom.fla
i have changed some modifications in the code.After tracing my code i am getting
[object TextField] in the output.But in the browser it is not focusing on the input textfield(username).Am i doing correct...

Plz find the below code for your reference.

Regards,
Naveen.
public function doLogin( event:MouseEvent ) : void 
                 {                                
                    //"userTextFieldInstanceName" is the username text field instance name
                    if(event.target.name == "loginButtonInstanceName"){
                     var username:String = loadedSM.userTextFieldInstanceName.text;
                     var password:String = loadedSM.passTextFieldInstanceName.text;
                    //stage.focus = loadedSM["userTextFieldInstanceName"];
                     stage.focus = loadedSM.userTextFieldInstanceName;
                     		    loadedSM.userTextFieldInstanceName.setSelectionloadedSM.userTextFieldInstanceName.text.length,
		    loadedSM.userTextFieldInstanceName.text.length);
                              
                               trace(stage.focus);
                               trace(password); 
                             
                    if(username.length<=0 || password.length<=0)
                    {
                              	Alert.show('username/password can not be empty');                         	
                    }
                     else{
                     		Alert.show('Continuing');
                    }
			                            
                               //now call your request with the username and password parameter in flex to validate login credentials
                       }
                       }

Open in new window

Okie,
The code is correct, just remove this line from flex:
stage.focus = loadedSM.userTextFieldInstanceName;

Instead write this javascript in html page and call the init function on load:
function thisMovie(movieName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
if (isIE) {
return window[movieName];
} else {
var embeds = document.body.getElementsByTagName('embed');
return embeds.item(0);
}
// function was:
// return (isIE) ? window[movieName] : document[movieName];
}
function init(){
    thisMovie(flashObject).focus();
}
onLoad="init()";

Open in new window


http://kb2.adobe.com/cps/155/tn_15586.html

Warm Regards
Deepanjan Das
Can u tell in which file i need to write this javascript code and also tell me from where this onLoad=init() is calling.

Paste the above javascript code in the html page within script tag in head.

Then place the onLoad function in body tage like this:

<body onLoad="init()";>

Warm Regards
Deepanjan Das
Sorry , paste this:

<body onLoad="init();">

Warm Regards
Deepanjan Das
i need to place this javascript in bin-debug\ main.html which is my mxml file or html-template\index.template.html

Regards,
Naveen.
Well, you may place this in your template file other wise every time you compile it will get over-written :)

Warm Regards
Deepanjan Das
After adding javascript in index.template.html and <body onLoad="init()">
I am getting the following error in Internet Explorer

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Timestamp: Thu, 24 Feb 2011 06:07:25 UTC

Message: 'flashObject' is undefined
Line: 45
Char: 5
Code: 0
URI: file:///D:/naveen.kumar/Documents/Flex%20Builder%203/hrhelpdesk/bin-debug/main.html


thisMovie(flashObject).focus();
Here in this line it is giving error

But when i comment the same code it is not showing the error and even not focusing in the textfield.
What might be the problem?

Regards,
Naveen.
Buddy :)
flashObject is the name of the flash object. So kindly replace that with the one that is mentioned in your html.

Warm Regards
Deepanjan Das
where & what i need to replace in place of flashObject? as i am not aware of flash very well.
can u give me the proper code for that.

Regards,
Naveen.
Okie, well kindly share the html page in bin-debug
I will place it for you.

Warm Regards
Deepanjan Das
Plz find the below code
<!-- saved from url=(0014)about:internet -->
<html lang="en">

<!-- 
Smart developers always View Source. 

This application was built using Adobe Flex, an open source framework
for building rich Internet applications that get delivered via the
Flash Player or to desktops via Adobe AIR. 

Learn more about Flex at http://flex.org 
// -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!--  BEGIN Browser History required section -->
<link rel="stylesheet" type="text/css" href="history/history.css" />
<!--  END Browser History required section -->

<title>${title}</title>
<script src="AC_OETags.js" language="javascript"></script>

<!--  BEGIN Browser History required section -->
<script src="history/history.js" language="javascript"></script>
<!--  END Browser History required section -->

<style>
body { margin: 0px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">

function thisMovie(movieName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
if (isIE) {
return window[movieName];
} else {
var embeds = document.body.getElementsByTagName('embed');
return embeds.item(0);
}
// function was:
// return (isIE) ? window[movieName] : document[movieName];
}
function init(){
thisMovie(flashObject).focus();
}

<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = ${version_major};
// Minor version of Flash required
var requiredMinorVersion = ${version_minor};
// Minor version of Flash required
var requiredRevision = ${version_revision};
// -----------------------------------------------------------------------------
// -->
</script>
</head>

<body scroll="no" onLoad="init()">
<script language="JavaScript" type="text/javascript">
<!--
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);

// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

if ( hasProductInstall && !hasRequestedVersion ) {
	// DO NOT MODIFY THE FOLLOWING FOUR LINES
	// Location visited after installation is complete if installation is required
	var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
	var MMredirectURL = window.location;
    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
    var MMdoctitle = document.title;

	AC_FL_RunContent(
		"src", "playerProductInstall",
		"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
		"width", "${width}",
		"height", "${height}",
		"align", "middle",
		"id", "${application}",
		"quality", "high",
		"bgcolor", "${bgcolor}",
		"name", "${application}",
		"allowScriptAccess","sameDomain",
		"type", "application/x-shockwave-flash",
		"pluginspage", "http://www.adobe.com/go/getflashplayer"
	);
} else if (hasRequestedVersion) {
	// if we've detected an acceptable version
	// embed the Flash Content SWF when all tests are passed
	AC_FL_RunContent(
			"src", "${swf}",
			"width", "${width}",
			"height", "${height}",
			"align", "middle",
			"id", "${application}",
			"quality", "high",
			"bgcolor", "${bgcolor}",
			"name", "${application}",
			"allowScriptAccess","sameDomain",
			"type", "application/x-shockwave-flash",
			"pluginspage", "http://www.adobe.com/go/getflashplayer"
	);
  } else {  // flash is too old or we can't detect the plugin
    var alternateContent = 'Alternate HTML content should be placed here. '
  	+ 'This content requires the Adobe Flash Player. '
   	+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
    document.write(alternateContent);  // insert non-flash content
  }
// -->
</script>
<noscript>
  	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="${application}" width="${width}" height="${height}"
			codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
			<param name="movie" value="${swf}.swf" />
			<param name="quality" value="high" />
			<param name="bgcolor" value="${bgcolor}" />
			<param name="allowScriptAccess" value="sameDomain" />
			<embed src="${swf}.swf" quality="high" bgcolor="${bgcolor}"
				width="${width}" height="${height}" name="${application}" align="middle"
				play="true"
				loop="false"
				quality="high"
				allowScriptAccess="sameDomain"
				type="application/x-shockwave-flash"
				pluginspage="http://www.adobe.com/go/getflashplayer">
			</embed>
	</object>
</noscript>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of deepanjandas
deepanjandas
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial