Solved

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

Posted on 2011-02-20
25
919 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
blocked plugin flash 5 107
Add 'birds singing' to homepage 5 90
Converting CFGRID from Flash to HTML in Coldfusion 1 84
Flash Converter 8 106
In this tutorial you will learn how to make an editable type treatment that resembles a neon sign. I'm going to assume that you have some familiarity with the appearance panel in Illustrator, but if not you can learn the basics by reading my other t…
In this article I share how to make a vector texture from a digital image. Vector textures are useful because they are scalable without losing in image quality. I'll be using Adobe Photoshop to adjust the highlights and contrast of a digital imag…
In this tutorial viewers will learn how to apply a non-rasterized, vector-scalable texture to a vector using opacity texture masks in Illustrator Open a vector in Illustrator. Black and white vectors work best, but this effect can also be used to ad…
In this second Micro Tutorial in the series,  viewers will learn more basic shortcuts and functions of Illustrator. In this tutorial you will learn about the eyedropper tool, zoom , ellipses and more.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now