Welcome!

Java Authors: Jim Kaskade, Carmen Gonzalez, Aater Suleman, Pat Romanski, Paige Leidig

Related Topics: Java, ColdFusion, Adobe Flex

Java: Article

Rich Internet Applications with Adobe Flex 2 and Java

Flash + POJO = RIAs

A typical Java developer knows that when you need to develop a GUI for a Java application, Swing is the tool. Eclipse SWT also has a number of followers, but the majority of people use Java Swing. For the past 10 years, it was a given that Swing development wouldn't be easy; you have to master working with the event-dispatch thread, GridBaglayout, and the like. Recently, the NetBeans team created a nice GUI designer called Matisse, which was also ported to MyEclipse. Prior to Matisse, JBuilder had the best Swing designer, but it was too expensive. Now a good designer comes with NetBeans for free.

Why even consider Flex for developing Rich Internet Applications (RIA)? First, we'll give the short answer. Just look at the code in Listing 1. This code compiles and runs in the Flash player and produces the output shown in Figure 1. Yes, it's a tree control with several nodes that know how to expand, collapse, and highlight the user's selection. Imagine the amount of Java code you'd need to write to achieve the same functionality.

The code is nice and clean; the GUI looks rich and appealing. The Flex compiler automatically converts the MXML code in Listing 1 into an object-oriented language called ActionScript, and then compiles it into an SWF file, the format that the Flash player understands.

To add the business processing to this example, we'd need to write event handlers in the ActionScript 3.0 language, which is very similar to Java, but we wouldn't have to worry about routing all events to the event-dispatch queue. Below are some of the other reasons that the Flex/Flash combination is a very promising technology for RIA development:

  • The Flash player, a powerful virtual machine with a high-performing byte code/JIT compiler and rich UI API, is available on most of the platforms.
  • The size of the VM is small.
  • It easily integrates with Web browsers.
  • Flash applications can run outside of the Web browser.
  • Flex offers component-based programming, which eliminates most of the low-level coding.
  • It offers simple integration with all kinds of media (video and audio). Java Swing is behind in this field.
  • It has a quick adoption rate as all other Adobe products.
  • Most important, Flex 2 easily integrates with Java on the server side.
Later in this article we'll develop a Stock Portfolio Application with Flex, and then we'll integrate it with POJOs.

Flex client applications are compiled SWF files that can be delivered to the client and run by the Flash player, which is installed as a plug-in to your browser. On the client side, Flex consists of a Flash player, a framework of predefined components, a couple of command-line compilers, and an Eclipse-based Flex Builder IDE. On the server side, Flex is a Web application that includes Flex Data Services and a Flex Charting component and can be deployed in any JEE server.

To see Flex in action, let's write the functional specification, then develop and deploy a sample application.

Designing the Stock Portfolio Application
We'll create a Web application that will receive and display a feed containing security prices and the latest news as in Figure 2 and 3.

The top portion of the screen must be populated by the stocks included in the user's portfolio. For simplicity, store the user's portfolio in the XML file as in Listing 2.

When the user clicks on a row with a particular stock (i.e., ADBE as in Figure 2), populate the lower data grid with the headlines related to the selected stock. The news should be coming from http://finance.yahoo.com/rss/headline. The column link has to contain the URLs of the news, and when the user clicks on the link, a new browser's window should pop up displaying the selected news article.

The top of the screen should contain the toggle buttons Show Grid/Show Chart. When the Show Grid option is selected, the user will see the screen as in Figure 2, and when Show Chart is selected, the top data grid has to be replaced with the pie chart (see Figure 3), preserving the same functionality (clicking on the pie slice repopulates the news headlines).The market data has to be refreshed on the screen every second or so.

Since we are limited by the size of this article, for the server side we'll just write a simple POJO that will generate random numbers and push them back to the Flash client. However, in the next article we'll add the data feed from an external Java application via JMS.

Sounds like an ambitious task for a short magazine article, doesn't it? This article is not a tutorial on Flex MXML or ActionScript, but as a Java developer, you should be able to understand how the code is put together with minimal explanations. After deploying and running this application  you can study Flex more formally. So let's roll up our sleeves...

Installing Flex Builder and Flex Data Services
At the time of this writing, Flex 2 is still in beta (http://labs.macromedia.com/flexproductline/ ) and its production release is expected this summer. The burning question is, "Is it free?" The answer is, "It depends." There is a free lunch (or rather a complimentary appetizer from the chef), but it'll just whet your appetite, and pretty soon your hand will start slowly reaching for the wallet. Flex Data Services (FDS) that provide seamless integration of Flex into enterprise data and messaging have deployment fees. For high-performance enterprise applications supporting client authentication, messaging, and the like, you may want to purchase the FDS license. For smaller applications, you might use integration via HTTPService, Web Services, and Remote Java invocation using standard and open source technologies.

The free part is Flex command-line compilers and the Flex framework that includes all these libraries of rich components. This means you can use any plain text editor for writing Flex code, compile it into the SWF file, and run it in a free Flash 9 player. As of today though, Adobe says that the Eclipse-based Flex Builder IDE, which makes development a lot more productive, will not be free. Flex Charting components that offer client-side interactive charts are not free either.

The Flex Builder IDE comes in two flavors: a standalone version and the Eclipse plug-in. You'll have to choose one of these versions during the installation process. Let's install Eclipse 3.1 first from www.eclipse.org/downloads/ (unless you have it already), and then the plug-in version of Flex Builder. During the installation, you'll have to specify the directory where your Eclipse resides (the one that has a subdirectory called plug-ins). After Flex Builder is installed, start the Eclipse IDE, select the menus Window | Open Perspective | Other and add the Flex Development perspective (the Flex Debugging perspective will be added automatically to the Eclipse IDE). The Flex Builder comes with great Help content, which in the plug-in version is hidden under the menu Help | Help Contents | Adobe Help.

To run client-only Flex applications, we need to install one more program: Flash Player 9. However, for our stock portfolio application, we'll need to install and deploy FDS, which is a Web application that can be deployed in any JEE compliant container. Since FDS is a Web application, you can deploy it in the application server of your choice. First, we'll download and install Apache Tomcat 5.5 from http://tomcat.apache.org/. By default, on Windows machines Tomcat is installed in "C:\Program Files\Apache Software Foundation\Tomcat 5.5" and runs on port 8080. At the end of the installation, the Apache Tomcat server will be started as a Windows service. Now download from and run the FDS executable installer (default directory ID C:\fds2), picking the optional Flex Data Services J2EE Application. After the installation is finished, unzip the content of the flex.war into Tomcat's webaps\ROOT directory and restart the Apache Tomcat service. Enter the URL http://127.0.0.1:8080/ in your browser, and you should see the FDS Welcome screen. Flex Data Services are deployed now under Tomcat.

Developing Stock Portfolio Application with Flex
What Runs Where
First, let's see which components will run on the client and what belongs to the Web server. The code shown in Listings 3-6 will be compiled into portfolio.swf, which will run on the client either independently in the Flash 8.5 player, or with the help of portfolio.html (auto-generated by Flex Builder) in the Web browser with the Flash plug-in. Tomcat is our server, which will host compiled Java classes shown in Listings 7 and 8. We'll also make small additions in the server configuration files (see Listings 9 and 10) to specify where to find the Java classes and to allow access to the external Yahoo! service. While Flex provides different ways of client/server communication (RemoteObjects, HTTPService, WebService), in this article our client will use the RPC by means of the <mx:RemoteObject> component that will find the matching Java classes located and configured on the server as described at the end of this article.

Developing the GUI Part
Open the Flex perspective in Eclipse and create a new project (e.g., Portfolio_RCP) containing files from Listings 3-7, and then compile it into one file called portfolio.swf. To simplify deployment, set the project's output directory to Tomcat's \ROOT\portfolio.

Our main file portfolio.mxml (see Listing 3) divides the screen with the vertical Flex box-type layout (<mx:VDividedBox>) with an adjustable divider between its children (think of Swing's split panes), and it includes two separate code fragments. The bottom child contains a data grid programmed in <FinancialNews> (see Listing 6). The top child contains <PortfolioView> (see Listing 4).

Java developers know that some Swing components store the data and GUI parts separately, for example, the JTable gets its feed from a data model class that can store its data in a Java collection. On a similar note, some Flex controls (e.g., <mx:DataGrid>) can also use ActionScript collection classes as data providers. Our data model is the file portfolio.xml depicted in Listing 2.

This code represents a typical master-detail relationship, where a change in the selected security in the master component <PortfolioView> repopulates the detail component <FinancialNews>.

Getting the Price Quotes
Let's go over some interesting programming techniques that we've applied while coding the top panel, which contains several Flex framework components wrapped into a Canvas container (see Listing 4). After learning that the user's portfolio is represented by portfolio.xml, you may expect at least some number of lines performing XML parsing.

What do you think of this line:

<mx:XML format="e4x" id="portfolioModel" source="portfolio.xml" />

The XML parsing is complete! After this line you can traverse the entire XML document using a simple dot notation, e.g., portfolioModel.security. This magic is possible because Flex supports the e4x format that was introduced by W3C as a simple alternative for reading and processing XML documents.

Another interesting feature to comment is data binding, which is a way to link the data in one object to another. In particular, it's a convenient way to move the data between the GUI and non-visual components. Data binding can be specified using the curly braces syntax. The following lines from Listing 4 provide an immediate refresh of the top data grid and chart as soon as the data in the underlying security changes.

<mx:DataGrid id="portfolioGrid" width="100%" height="100%"
      dataProvider="{portfolioModel.security}"
<mx:PieChart id="portfolioPie" dataProvider="{portfolioModel.security}"

The variable portfolioModel.security represents the data source, and the destination will change as soon as any property of the source changes. Note that we have one data source mapped to two destinations (the data grid and pie chart), so we don't need to do any additional programming to refresh the data grid and the chart when the incoming market data changes the properties of the security.


More Stories By Victor Rasputnis

Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at [email protected]

More Stories By Yakov Fain

Yakov Fain is a co-founder of two software companies: Farata Systems and SuranceBay. He authored several technical books and lots of articles on software development. Yakov is Java Champion (https://java-champions.java.net). He leads leads Princeton Java Users Group. Two of Yakov's books will go in print this year: "Enterprise Web Development" (O'Reilly) and "Java For Kids" (No Starch Press).

More Stories By Anatole Tartakovsky

Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at [email protected]

Comments (17)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
SYS-CON Events announced today that Harbinger Systems will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Harbinger Systems is a global company providing software technology services. Since 1990, Harbinger has developed a strong customer base worldwide. Its customers include software product companies ranging from hi-tech start-ups in Silicon Valley to leading product companies in the US and large in-house IT organizations.
SYS-CON Events announces a new pavilion on the Cloud Expo floor where WebRTC converges with the Internet of Things. Pavilion will showcase WebRTC and the Internet of Things. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devices--computers, smartphones, tablets, and sensors – connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades.
The only place to be June 9-11 is Cloud Expo & @ThingsExpo 2015 East at the Javits Center in New York City. Join us there as delegates from all over the world come to listen to and engage with speakers & sponsors from the leading Cloud Computing, IoT & Big Data companies. Cloud Expo & @ThingsExpo are the leading events covering the booming market of Cloud Computing, IoT & Big Data for the enterprise. Speakers from all over the world will be hand-picked for their ability to explore the economic strategies that utility/cloud computing provides. Whether public, private, or in a hybrid form, clo...
SYS-CON Events announced today that Gridstore™, the leader in software-defined storage (SDS) purpose-built for Windows Servers and Hyper-V, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Gridstore™ is the leader in software-defined storage purpose built for virtualization that is designed to accelerate applications in virtualized environments. Using its patented Server-Side Virtual Controller™ Technology (SVCT) to eliminate the I/O blender effect and accelerate applications Gridsto...
SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at Internet of @ThingsExpo, Andrew Bolwell, Director of Innovation for HP’s Printing and Personal Systems Group, will discuss how key attributes of mobile technology – touch input, senso...
The Internet of Things (IoT) is making everything it touches smarter – smart devices, smart cars and smart cities. And lucky us, we’re just beginning to reap the benefits as we work toward a networked society. However, this technology-driven innovation is impacting more than just individuals. The IoT has an environmental impact as well, which brings us to the theme of this month’s #IoTuesday Twitter chat. The ability to remove inefficiencies through connected objects is driving change throughout every sector, including waste management. BigBelly Solar, located just outside of Boston, is trans...
Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, will examine three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective storage designed to handle the massive surge in back-end data in a world where timely analytics...
Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
The Internet of Things (IoT) promises to evolve the way the world does business; however, understanding how to apply it to your company can be a mystery. Most people struggle with understanding the potential business uses or tend to get caught up in the technology, resulting in solutions that fail to meet even minimum business goals. In his session at Internet of @ThingsExpo, Jesse Shiah, CEO / President / Co-Founder of AgilePoint Inc., will show what is needed to leverage the IoT to transform your business. He will discuss opportunities and challenges ahead for the IoT from a market and tec...
SYS-CON Events announced today that TeleStax, the main sponsor of Mobicents, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. TeleStax provides Open Source Communications software and services that facilitate the shift from legacy SS7 based IN networks to IP based LTE and IMS networks hosted on private (on-premise), hybrid or public clouds. TeleStax products include Restcomm, JSLEE, SMSC Gateway, USSD Gateway, SS7 Resource Adaptors, SIP Servlets, Rich Multimedia Services, Presence Services/RCS, Diame...
From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.
SYS-CON Events announced today that O'Reilly Media has been named “Media Sponsor” of SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. O'Reilly Media spreads the knowledge of innovators through its books, online services, magazines, and conferences. Since 1978, O'Reilly Media has been a chronicler and catalyst of cutting-edge development, homing in on the technology trends that really matter and spurring their adoption by amplifying "faint signals" from the alpha geeks who are creating the future. An...
SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
The Transparent Cloud-computing Consortium (abbreviation: T-Cloud Consortium) will conduct research activities into changes in the computing model as a result of collaboration between "device" and "cloud" and the creation of new value and markets through organic data processing High speed and high quality networks, and dramatic improvements in computer processing capabilities, have greatly changed the nature of applications and made the storing and processing of data on the network commonplace.
The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
As a disruptive technology, Web Real-Time Communication (WebRTC), which is an emerging standard of web communications, is redefining how brands and consumers communicate in real time. The on-going narrative around WebRTC has largely been around incorporating video, audio and chat functions to apps. In his session at Internet of @ThingsExpo, Alex Gouaillard, Founder and CTO of Temasys Communications, will look at a fourth element – data channels – and talk about its potential to move WebRTC beyond browsers and into the Internet of Things.
SYS-CON Events announced today that Gigaom Research has been named "Media Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Ashar Baig, Research Director, Cloud, at Gigaom Research, will also lead a Power Panel on the topic "Choosing the Right Cloud Option." Gigaom Research provides timely, in-depth analysis of emerging technologies for individual and corporate subscribers. Gigaom Research's network of 200+ independent analysts provides new content daily that bridges the gap between break...
We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
The Industrial Internet revolution is now underway, enabled by connected machines and billions of devices that communicate and collaborate. The massive amounts of Big Data requiring real-time analysis is flooding legacy IT systems and giving way to cloud environments that can handle the unpredictable workloads. Yet many barriers remain until we can fully realize the opportunities and benefits from the convergence of machines and devices with Big Data and the cloud, including interoperability, data security and privacy.