How to invoke Java methods from Flex application?
- Add an mapping to the Java class to services-config.xml
- Map your RemoteObject (provided with Flex SDK) on the client side to the destination configured in services-config.xml
- Invoke the method in your Java class using the RemoteObject instance
What do you need to invoke a Java method from the client side application?
- Flex Builder
- Blaze DS
Isn’t that simple? Let’s get into details of implementing this.
Set up Blaze DS
Download and setup Blaze DS on your system fromhttp://labs.adobe.com/technologies/blazeds/
You will be downloading Blaze DS with integrated Tomcat server. You just have to start the server by executing startup.bat under tomcat/bin. Please note the port number in which the Tomcat server is running. The port number mentioned below might have to be replaced with yours, if required. Continue reading ‘Invoking Java methods from Adobe Flex’
Panel 1 is a column chart declared in the main application and Panel 2 is the exact same column chart in a module. There is an embedded font ‘Verdana’ in the main application.
Charting.mxml
<?xml version=”1.0? encoding=”utf-8??>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”horizontal” xmlns:local=”*”>
<mx:Style>
Application
{
font-family: Verdana;
}
@font-face {
src:url(”font/verdana.ttf”);
fontFamily: Verdana;
advancedAntiAliasing: true;
}
@font-face {
src:url(”font/verdanab.ttf”);
fontFamily: Verdana;
fontWeight: bold;
advancedAntiAliasing: true;
}
@font-face {
src:url(”font/verdanai.ttf”);
fontFamily: Verdana;
font-style: italic;
advancedAntiAliasing: true;
}
</mx:Style>
<local:ColumnChart width=”50%” height=”100%” title=”Chart 1?/>
<mx:ModuleLoader url=”ChartModule.swf” width=”50%” height=”100%”/>
</mx:Application>
It’s tax season here in the US until April 15th. I decided to get an early start. I opened up TurboTax(tax software). After entering some numbers, you will notice the little “Federal Refund” ticker at the top that keeps a running total of the money you will get, or owe, based on all the numbers entered. The refund ticker also has a cool animation on it when it goes up or down. I quickly got bored of doing my taxes and instead decided to write the little number changer component in Flex.
<?xml version=”1.0? encoding=”utf-8??>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical”>
<mx:Script>
<![CDATA[
[Bindable] private var currentNumber:int = 0;
private var difference:Number;
private function buttonClick():void
Continue reading ‘Creating a Number Change Effect in Flex’
The class uses javascript to get all of the url information, including host name, port, and url parameters among other things. The most helpful method you will find is getParameterValue(key:String):String which will give you back the parameter value for the given key.
Have multiple environments and need to get the full url with the host name and port in actionscript?
There are several methods to help with this:
getHostName()
getPort()
getProtocol() -such as http or https
getContext() -The path after the hostname but before the url parameters
Attached is the source. You can see that the methods are all static and use javascript to get the information back to flex. It is designed to get this information regardless of where the flex swf file lives since most people have them embeded in an html file.
This example is associated with the blog “Dustin’s Software
Cogitations and Speculations.”
The Flash Players, regular and debugger, can be downloaded
at http://www.adobe.com/support/flashplayer/downloads.html. –>
<mx:Script>
import mx.utils.ObjectUtil;
/**
* Test Flex exception handling.
*/
public function testException():void
{
try
{
intentionallyThrowException();
}
catch (error:Error)
{
stackTraceText.text = ObjectUtil.toString(error.getStackTrace());
messageText.text = error.message;
toStringText.text = error.toString();
nameText.text = error.name;
errorIdText.text = String(error.errorID);
}
finally
{
// I get called whether an exception is caught or not.
}
}
/**
* Intentionally throw an exception for use in Flex exception
* testing.
*/
public function intentionallyThrowException():void
{
throw new SyntaxError(“That was some bad syntax!”);
}
</mx:Script>
<mx:VBox id=”mainDisplay”>
<mx:Form>
<mx:FormItem id=”stackTraceItem” label=”Exception Stack Trace”>
<mx:Text id=”stackTraceText” />
</mx:FormItem>
<mx:FormItem id=”messageItem” label=”Message”>
<mx:Text id=”messageText” />
</mx:FormItem>
<mx:FormItem id=”toStringItem” label=”toString”>
<mx:Text id=”toStringText” />
</mx:FormItem>
<mx:FormItem id=”nameItem” label=”Name”>
<mx:Text id=”nameText” />
</mx:FormItem>
<mx:FormItem id=”errorIdItem” label=”Error ID”>
<mx:Text id=”errorIdText” />
</mx:FormItem>
</mx:Form>
</mx:VBox>
</mx:Application>
A significant portion of the above code is actually comments. If you remove the explanatory comments, the code is pretty small. Either way, the code is straightforward, especially if you have written Java exception handling code before. Continue reading ‘Flex Error Handling Example’