JavaFX 1.0 launched - access services hosted on embedded GlassFish
Today Sun announces the availability of Java
FX 1.0.
JavaFX 1.0 is a rich client platform for creating and delivering Rich
Internet Applications across all screens (desktop, browser,
and mobile) of your life. It consists of the following key components:

|
- JavaFX
SDK includes JavaFX script compiler and runtime
tools, and a host of libraries to create RIAs for desktop, browser and
mobile platforms, command-line tools & Ant tasks and other
goodies.
- NetBeans
6.5 support (as plugin or bundled with IDE) that allows to
build, preview and debug JavaFX applications using NetBeans IDE. If you
prefer CLI support then SDK can be downloaded.
- Production
Suite is a suite of tools and plugins for creative tools
(such as Illustrator CS3+) that allows graphical assets to be
exported to JavaFX applications.
|
The beauty of JavaFX is that its fully integrated with the Java Runtime
and takes advantage of the performance and ubiquity of Sun's Java
Runtime Environment that is installed on literally billions of devices
worldwide. Hence, JavaFX applications will run on any desktop, browser,
mobile device or any other connected device that runs the Java Runtime
Environment.
 |
This blog shows how to create a simple JavaFX
application using NetBeans
IDE. The application plays a movie, allows the
viewer to cast a vote if they liked it, and see aggregate response from
other viewers. The application is developed using NetBeans 6.5, JavaFX
1.0 plugin, and coded using JavaFX Script. The voting engine
is deployed as a RESTful Web service using Jersey on GlassFish. |
In terms of
user experience, running the NetBeans project shows a window playing
the movie. The first mouse hover over the window allows the viewer to
click
on "I love it" or "Not so great" and cast their vote as shown below:
Any subsequent
mouse hover shows aggregated
results from other viewers as shown below:
The results are not interesting if there is a single viewer of the
movie. But for a production environment, this movie will be played by
multiple users concurrently and the percentage numbers will be more
meaningful. You can close the window and run the project again to vote
again, as many times as you like :)
For those who like to see quick results, here is a 4-step guide to get
started:
- In NetBeans 6.5 IDE, install JavaFX plugin as explained here and RESTful
Web services plugin as explained here. Both the
plugins may be installed in one step by selecting the required plugins
together.
- Download NetBeans project for JavaFX client from here
and add Jersey dependencies as explained in bullet #5
below.
- Download Web service endpoint Maven project from here
and deploy the endpoint as explained in bullet
#4 below.
- Run the JavaFX application as explained here.
The remainder of this blog explains the details and shows how to
construct the demo from scratch.
Lets first create the JavaFX application that plays the video movie.
- In NetBeans
6.5, install "JavaFX SDK" plugin. In
the "Tools"
menu, "Plugins", search on "JavaFX", select "JavaFX SDK" and click on
"Install".
- Create a new project of type "JavaFX", "JavaFX Script
Application". Take the default values as shown below:

and click on "Finish".
- The source code for this class can be downloaded from here
or alternatively constructed as explained in the sub-bullets.
- In the newly created class, change the Stage (root area
for
all scene content) to:
Stage {
title: "GlassFish Media Player"
width: 625
height: 360
resizable: false
scene:
myScene
} |
- Create a scene that contains the view of the media to be
played and controls the display of the Vote or Result nodes:
var myScene: Scene = Scene {
content: MediaView {
fitWidth: 625
fitHeight: 360
mediaPlayer:
bind myPlayer
onMouseEntered: function( e: MouseEvent ):Void {
println("mouse entered");
if (voted == false) {
insert Vote{} into
myScene.content;
} else {
insert
Result{}
into myScene.content;
}
}
onMouseExited: function( e: MouseEvent ):Void {
delete myScene.content[1]
}
}
} |
- Create a Media Player to use with the scene:
var myPlayer: MediaPlayer = MediaPlayer{
autoPlay: true
media:
bind myMedia
}; |
- Create the media object to be used with the Media Player:
var myMedia: Media = Media {
source:
"http://sun.edgeboss.net/download/sun/media/1460825906/1460825906_2957290001_DayEarth-Bluray.flv"
}; |
You can change the location of the movie here in the media player. For
example, changing it to
"http://mediacast.sun.com/users/ArunGupta/media/v3prelude-nb65-webapp.flv"
will start playing the screencast
#27.
- Create a Vote class that is a CustomNode and appears when
a
user's mouse enters the scene where the video is playing. The user can
select whether he likes the clip or not and the vote is recorded
making a Web service call using Jersey
Client APIs:
class Vote extends CustomNode {
override function create():Node {
return
Group {
content: [
Rectangle {
fill: Color.GREEN
x: 185
y: 145
width: 243
height: 38
arcWidth: 20
arcHeight: 20
},
Text {
x: 195
y: 170
fill: Color.WHITE
font: Font {
size: 18
}
content: "I love it"
},
Rectangle{
x: 191
y: 148
smooth: false
width: 73
height: 32
fill: Color.TRANSPARENT
onMouseClicked: function( e: MouseEvent ):Void {
println("clicked I love it");
voted = true;
wsClient.voteLoveIt();
delete myScene.content[1]
}
},
Text{
x: 305
y: 170
fill: Color.WHITE
font: Font {
size: 18
}
content: "Not so great"
},
Rectangle {
x: 301
y: 148
smooth: false
width: 118
height: 32
fill: Color.TRANSPARENT
onMouseClicked: function( e: MouseEvent ):Void {
voted = true;
println("clicked Not so great");
wsClient.voteNotSoGreat();
delete myScene.content[1]
}
}
]
}
}
}; |
- Create a Result class that is a CustomNode and simply
reports on how many voters like this clip:
class Result extends CustomNode {
override function create():Node {
var resultPercent =
wsClient.showResults();
var resultString =
"{resultPercent} voters liked this clip";
return
Group {
content: [
Rectangle {
fill: Color.BLUE
x: 187
y: 145
width: 244
height: 38
arcWidth: 20
arcHeight: 20
onMouseClicked: function( e: MouseEvent ):Void {
delete myScene.content[1]
}
},
Text {
x: 199
y: 170
fill: Color.WHITE
font: Font {
size: 18
}
content:
resultString
}
]
}
}
}; |
- Add two instance variables:
var voted = false;
var wsClient = new WebserviceClient; |
The first variable captures if the viewer has already voted and the
second variable is an instance to the RESTful Web service client.
- Add the following import statements:
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage; |
"Fix Imports" should be able to fix them and bug
#154307 is already filed for that.
- Create a new class that is used to capture the Vote as:
@javax.xml.bind.annotation.XmlRootElement
public class VoteBean {
public static enum VOTE { LOVE_IT,
NOT_SO_GREAT };
public VOTE vote;
public VoteBean() { vote = VOTE.LOVE_IT;
}
public VoteBean(VOTE vote) {
this.vote = vote;
}
} |
This is a simple Javabean with a standard JAXB annotation.
This ensures that XML is used as the data format for transfering
results between client and endpoint. The source code for this class is
available here.
- Add
Jersey libraries to the project by right-clicking on
Project, select Libraries, click on "Add Library...", select "JAX-RS
1.0" and "Jersey 1.0 (JAX-RS RI)", and click on "Add Library".

If these libraries
are not available then install the "RESTful Web
Services" plugin from the Plugin Center.
- And finally add the class that invokes the RESTful
Webservice
endpoint:
public
class WebserviceClient {
private static com.sun.jersey.api.client.WebResource
createWebResource() {
return com.sun.jersey.api.client.Client.create().
resource("http://localhost:8080/movie-feedback-webapp/webresources/myresource");
}
public static void voteLoveIt() {
createWebResource().type("application/json").
post(new
VoteBean(VoteBean.VOTE.LOVE_IT));
}
public static void voteNotSoGreat() {
createWebResource().type("application/json").
post(new
VoteBean(VoteBean.VOTE.NOT_SO_GREAT));
}
public static String showResults() {
return createWebResource().get(String.class);
}
} |
The Webservice endpoint will be hosted at
"http://localhost:8080/movie-feedback-webapp/webresources/myresource".
A WebResource is created from the Client. The POST methods are used to
cast the user vote and GET method is used to retrieve the aggregated
results. The source code for this class is available here.
Now lets create the RESTful endpoint using Jersey and deploy on
GlassFish.
- Create and deploy a RESTful Web service endpoint
- Create a template RESTful Web service endpoint as
described in TOTD
#56. Lets use the artifactId as "movie-feedback-webapp".
- Create the bean "VoteBean" in "org.glassfish.samples"
package. This is the exactly same bean used earlier by the client:
@javax.xml.bind.annotation.XmlRootElement
public class VoteBean {
public static enum VOTE { LOVE_IT,
NOT_SO_GREAT };
public VOTE vote;
public VoteBean() { vote = VOTE.LOVE_IT;
}
public VoteBean(VOTE vote) {
this.vote = vote;
}
}
|
- Update the generated resource
- Add @com.sun.jersey.spi.resource.Singleton
as class annotation so that only one instance of the resource is
created for the entire web
application. This allows to save state (preferences from other users)
in the RESTful resource.
- Add two instance variables:
int loveIt;
int noSoGreat; |
- Add a method that will process HTTP POST requests as:
@POST
public void postOneVote(VoteBean bean) {
if (bean.vote == VoteBean.VOTE.LOVE_IT) {
loveIt++;
} else {
noSoGreat++;
}
System.out.println("In POST: " + bean.vote);
} |
This method stores the vote in the resource. The handling of POST
request messages by Jersey is explained in TOTD
#58.
- Add a method that will process HTTP GET requests as:
@GET
@Produces("text/plain")
public String getOpinion() {
if (loveIt == 0 && noSoGreat == 0)
return "No votes cast yet!";
return (loveIt * 100) / (loveIt + noSoGreat) + "%";
} |
This method calculates the percentage of viewers who liked the movie.
- Deploy the
endpoint using "mvn glassfish:run" in
"movie-feedback-webapp" directory.
Now run the JavaFX application by
right-clicking on the project and
selecting "Run Project" and start voting! The percentage
results will vary if the movie is voted upon more than once.
This blog showed:
- How to install JavaFX capabilities to an existing NetBeans
6.5 installation
- How to create a simple JavaFX application that plays media
files
- Integrate it with existing Java libraries (Jersey client
libraries in this case)
- Invoke services hosted on GlassFish
The steps followed in this blog allows for rapid development/debugging
of JavaFX application accessing resources using embeddable GlassFish
but are not ideal for production deployments. A future
blog will show how this JavaFX application can be deployed as a
Java
Web Start application and scaled for mulitple users.
The
javafx.com/samples
has loads of samples and
javafx.com/tutorials
shows how to build your own applications. The
JavaFX
Community Wiki is a great place to collaborate.
Send your Jersey questions to
users@jersey.dev.java.net,
GlassFish questions to
GlassFish
Forum, and JavaFX questions to
JavaFX
Forums.
Technorati:
glassfish
v3
jersey
webservices
javafx netbeans
Posted
by Arun Gupta in webservices |

|

|

|

|

|

|
|
Pity it is not available for Linux/Ubuntu,
Why release JavaFX for windows before releasing it for Linux and Solaris?
Posted by Wim Flory on December 04, 2008 at 12:20 PM PST #
Linux and Solaris support are being actively worked upon. Please read more details at:
http://blogs.sun.com/javafx/entry/a_word_on_linux_and
Posted by Arun Gupta on December 04, 2008 at 12:30 PM PST #
You want to have quick return around your business with new technology and quality way so get concerd to Real assistance.com <a href="http://www.realdataassistance.com/">Data entry service providers</a>
Posted by Robert on December 04, 2008 at 11:04 PM PST #
Great tutorial, and it almost looks like javafx is not that silly, as it sounded when we first heard the announcement. I'm still waiting for a compelling reason to use it instead of flex/gwt/your-favorite-ajax-framework, but at least you can get your job done with fx, too.
PS: May I propose a printing-style-sheet that switches of the right navigation-bar!? I love your tutorials and would love them even more if I could print them and read them over breakfast...
Posted by Stefan on December 08, 2008 at 11:12 PM PST #
I can't see the video also I can here the sound.
Posted by Robert on December 09, 2008 at 05:42 AM PST #
Stefan, good suggestion and I'll try to incorporate it.
Robert, are you connected to Internet when playing the video ? Is there any error in the console ?
Posted by Arun Gupta on December 09, 2008 at 07:01 AM PST #
Yes, I have a working Internet connection and there are no errors in the console. And I'm hearing sound.
Posted by Robert on December 09, 2008 at 11:43 AM PST #
Hmm, the location is embedded in src/javafxapplication1/Main.fx and is ""http://sun.edgeboss.net/download/sun/media/1460825906/1460825906_2957290001_DayEarth-Bl
uray.flv". Can you play the video directly in your browser ?
Anything beyond that, you may have to ask your question on JavaFX forums:
http://forums.sun.com/category.jspa?categoryID=132
Posted by Arun Gupta on December 09, 2008 at 12:14 PM PST #
ThankSs
Posted by dizi izle on February 21, 2009 at 09:23 AM PST #
thx u.
Posted by sinema izle on March 07, 2009 at 03:38 PM PST #
Hey Arun - do you know if the Jersey client stuff works on JavaFX Mobile???
Posted by Pat Patterson on April 22, 2009 at 10:52 AM PDT #
Pat, I've not tried and Google also does not show up anything. Try asking the question on users@jersey.
Posted by Arun Gupta on April 22, 2009 at 11:04 AM PDT #
To answer my own question - no - Jersey client doesn't work on JavaFX Mobile - my test program (working on JavaFX Desktop) fails with "class file for java.net.URI not found". Unsurprising, since java.net.* is not in Java ME. Oh well - back to javafx.data.pull.PullParser.
Posted by Pat Patterson on April 28, 2009 at 02:36 PM PDT #