Solved

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

Posted on 2011-02-20
25
940 Views
Last Modified: 2012-06-27
How to focus a textInput Field by default in Flash as3?

Regards,
Naveen.
0
Comment
Question by:naveenm_006
  • 12
  • 12
25 Comments
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34940353
stage.focus = textField;//textField is the name of the textField instance.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:naveenm_006
ID: 34940404
But i am not getting the desired o/p that is not focusing on the textInputField.
sampark-bottom.fla
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34940450
Try this:
sampark-bottom.fla

Warm Regards
Deepanjan Das
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:naveenm_006
ID: 34940684
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
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34941057
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
0
 

Author Comment

by:naveenm_006
ID: 34941081
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]
0
 

Author Comment

by:naveenm_006
ID: 34950598
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]
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34951025
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
0
 

Author Comment

by:naveenm_006
ID: 34957945
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]
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34957967
Can you please pass be the mxml file and sampark-bottom.fla

Warm Regards
Deepanjan Das
0
 

Author Comment

by:naveenm_006
ID: 34958042
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
0
 

Author Comment

by:naveenm_006
ID: 34958813
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

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34960699
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
0
 

Author Comment

by:naveenm_006
ID: 34967198
Can u tell in which file i need to write this javascript code and also tell me from where this onLoad=init() is calling.

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34967277
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
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34967280
Sorry , paste this:

<body onLoad="init();">

Warm Regards
Deepanjan Das
0
 

Author Comment

by:naveenm_006
ID: 34967315
i need to place this javascript in bin-debug\ main.html which is my mxml file or html-template\index.template.html

Regards,
Naveen.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34967395
Well, you may place this in your template file other wise every time you compile it will get over-written :)

Warm Regards
Deepanjan Das
0
 

Author Comment

by:naveenm_006
ID: 34967513
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.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34967622
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
0
 

Author Comment

by:naveenm_006
ID: 34967694
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.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34967809
Okie, well kindly share the html page in bin-debug
I will place it for you.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:naveenm_006
ID: 34967838
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

0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 50 total points
ID: 34967912
You have sent me the html-template:

function init(){
    thisMovie(${application}).focus();
}

Or in the html in bin-debug, use this:

function init(){
    thisMovie(objectName).focus();
    //objectName is the name value of the parameter "name" or "id"
}

Warm Regards
Deepanjan Das
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Skew Text In Photoshop 11 1,945
How  to reduce the .AVI file size of 90GB 4 351
jpeg to Word Converter Recommendation 4 181
Indesign Data Merge 1 195
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
In this tutorial viewers will learn how to create a vector texture to apply a non-rasterized texture to vector images, using Photoshop and Illustrator Open a textured image such as a scanned-in piece of cardboard in Photoshop: Convert the texture to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question