JavaFX 2.0 Introduction by Example book

19 12 2011

I recently finished writing a book on the new release of the JavaFX 2.0 SDK and it has already been placed on the shelves at a bookstore (Amazon) near you. The book will walk you through, step-by-step, giving you the ins and outs of JavaFX 2.0. When you encounter a chapter you will be presented recipes which will pose a problem (use case scenario) that will have an associated solution. After a proposed solution you will be shown an example source code listing and its display output after the program has been executed. Lastly you will be shown a section called “How it works” which will explain and discuss the examples and their details. To see more about this book, such as the source code and errata, please visit Apress Publishing (http://www.apress.com/9781430242574).

In this blog entry I also want to give you a sneak preview of a Java Webstart example of chapter 3 recipe 3-1 (JavaFX 2.0 MP3 Player). To launch the JavaFX MP3 player just jump down to Chapter 3 below.

Below is a brief overview of the chapters in the book:

Chapter 1: JavaFX Fundamentals

I begin by instructing you on how to get your environment set-up to rapidly develop rich internet applications using Java and JavaFX.

JavaFX Swiss army knife

After reading this chapter you will be able to answer questions such as:

  • How do I create GUI applications?
  • What is the Scene graph?
  • How do I display text onto the scene graph?
  • How do I incorporate UI controls into my application?
  • How do I bind expressions?
  • How do I draw shapes?
  • How do I generate a background process?
  • How do I associate keyboard sequences to applications?

Chapter 2: Graphics with JavaFX
In chapter 2 there are recipe examples which delve into JavaFX’s graphics and animation capabilities. I begin by explaining some of the basics of JavaFX’s Graphics such as rendering images and animating nodes. I then go on to more advanced topics such as animating with transitions, manipulating layouts, and enhancing nodes using JavaFX CSS.
The following picture, taken from Recipe 2-2, depicts an image viewer application with a custom news ticker control at the bottom.

Photo Viewer and News Ticker application

Photo Viewer and News Ticker application

Chapter 3: Media with JavaFX
Chapter 3 covers all things media related. Here I reveal JavaFX’s media APIs which allows you to integrate audio and video into your applications. I begin by showing you how to create a JavaFX MP3 player and a video player. Then I will walk you through the different ways to enhance the video player with additional features to control media actions and events, mark positions in a video, and synchronize animations.

The illustration below, taken from Recipe 3-1, depicts a JavaFX 2.0 MP3 player with a graphical visualization (using the AudioSpectrumListener API). Before launching the example you will need to know the requirements and instructions to run the demo example.

JavaFX 2.0 MP3 Player

JavaFX MP3 Player

Simple requirements and instructions to run the JavaFX 2.0 MP3 Player:

Requirements

  • Java 6 update 24 JRE or greater (Java 7 is preferred)
  • JavaFX 2.0 or greater (JavaFX 2.0.2 is preferred)
  • Windows XP SP 3 or greater. (I will update the jnlp as JavaFX becomes available on other OSes)

Instructions

  1. Click the Webstart launch button below.
  2. By using your file explorer on your host operating system “drag and drop” a music mp3 file onto the surface of the application.
  3. Use the controls to the bottom right of the application to pause, play, and stop the music.
  4. You may use your mouse to drag the application around your desktop.
  5. To close the application click on the ‘X’ in the upper right hand corner.

To launch the application click on the Java Webstart button below:

Demo JavaFX 2.0 MP3 Player

Chapter 4: JavaFX on the Web
In chapter 4 you will be able to take advantage of the interoperability between JavaFX and HTML5.

For starters I will cover how to embed JavaFX applications into a Web page. I then will demonstrate JavaFX’s powerful WebView and WebEngine APIs. Below are the recipe examples from chapter 4 which utilize the  WebView and WebEngine APIs:

  • Displaying HTML5 Content (animated analog clock application)
  • Loading data from a Web service (weather application)
  • Handling Web events
  • Storing and displaying data using an embedded database (RSS reader application)

Depicted below is an animated analog clock application, taken from Recipe 4-2, demonstrating the ability to render HTML5 content.

JavaFX 2.0 Analog Clock

JavaFX 2.0 Analog Clock (HTML5)

I assume you know the Java programming language and some web development concepts. I hope you will enjoy these examples which can be used freely in your own projects. I’ve tested the examples with the latest Java 7 update 2 and JavaFX 2.0.2 runtime and SDK. If you have any questions or comments feel free to ask them here or on my Twitter account @carldea .

Thanks!

Carl

About these ads

Actions

Information

18 responses

27 12 2011
dejialadejebi

Hi, I am ordering your book from Amazon. However, I do have this general concern about javafx against html5. I have blogged about it here (http://buzzingminds.wordpress.com/2011/12/02/my-thoughts-on-how-javafx-can-survive-the-coming-html5-blast/) . I want to know what you think and why you will still advice that java developers should concentrate on JavaFX.

28 12 2011
Java desktop links of the week, December 28 | Jonathan Giles

[...] Carl Dea, who recently published the ‘JavaFX 2.0 Introduction by Example‘ book, has published a sample on his blog. [...]

28 12 2011
JavaFX links of the week, December 28 // JavaFX News, Demos and Insight // FX Experience

[...] Carl Dea, who recently published the ‘JavaFX 2.0 Introduction by Example‘ book, has published a sample on his blog. [...]

28 12 2011
carldea

Deji,

Thank you for getting my book. I think you shouldn’t have any concerns or worries about JavaFX competing with HTML5. I don’t see them working against each other at all, but rather working together to complement one another.

Here are some nice things JavaFX 2.0 (aka Java 7 update 2) can provide:
1) 3D
2) Charting API (out of the box experience)
3) Cross platform media player (sound and video)
4) Native drag and drop support
5) Extensible and Skinnable controls (out of the box experience)
6) Controls and other node types coexist with animation APIs.
7) Ready layout managers for well behaved UIs.(out of the box experience)
8) Tray icon API, right click, wheel mouse (any other desktop related metaphors API)
9) Swing interop
10) SWT interop
11) Bindings API
12) Effects API (out of the box experience)
13) Html Editor control
14) Irregular shaped windows with transparency
15) Easier to test and debug. (JUnit, etc.)
16) Allowing jvm languages (Jthon, JRuby, Groovy, Scala, Visage, Closure) to build awesome GUIs.
17) Take advantage of Java Webstart & JNLP APIs (offline mode, persistence, security)
18) Takes advantage of the graphics hardware on Mac, Windows, and soon on Linux.
19) Jitted…
20) FXML for standards and tooling.
21) Java’s core is available (Concurrency APIs, NIO, etc.)
22) Can interact with HTML5/CSS/SVG/JavaScript
23) Can render HTML5 Content

You may ask the question “What are the many things JavaFX begins to provide where HTML5 stops?”. An important principle that often comes to mind when solving problems is using the right tool for the right job. Also it depends on the problem you are really trying to solve. I believe JavaFX currently is a perfect fit for enterprise applications, casual games, media players, mini rcp tools apps, photo editing application, kiosk apps, install wizards, training CBTs, and many offline applications that need synching when near a wifi hotspot. In the case with Java 7 update 2 having JavaFX included means that you can develop amazingly rich client apps using all your existing Java skills (and html5, CSS, Javascript) , investment, and infrastructure.

We all know that not all browsers are created equal. So, it is often difficult to build reliable software that runs in a more predictable way. I believe JavaFX/Java is an ideal way to build rich applications and it is here to stay. JavaFX may not be popular compared to HTML5 but being popular doesn’t mean it will solve every use case. However when it comes to adoption I do feel JavaFX will need to work very hard on its deployment story (jre, applets, etc) and get things right.

Recent interviews with James Weaver which may help you decide if JavaFX is for you:
http://blogs.oracle.com/javaspotlight/entry/java_spotlight_episode_61_jim
http://learnjavafx.typepad.com/weblog/2011/12/brief-javafx-20-video-interview-from-javaone-latin-america.html

Thanks!
Carl

29 12 2011
dejialadejebi

Thanks Carl for your reply. I really appreciate it. I have one last question however which is still what bothers me the most. What do you think is the JavaFX community plans to make JavaFX work on tablet and mobiles. I think thats my biggest question. I have no doubts that JavaFX will rule as long as we have desktops but any immediate or future plans for the mobile space? thanks

31 12 2011
carldea

Deji,

To be honest with you I don’t really know what the future holds. Based on the history of many companies that own great technology will often position themselves where they can navigate through a legal landscape (maze, land mines).

I believe Oracle has met and delivered on many milestones (promises). At the rate they are going things look great for RIA and desktop development. Yes, it would be wonderful to see Apple relax licensing to allow Java/JavaFX to run on iPad and iPhone devices. This would usher in a huge developer mind share. I also believe JavaFX on the Android OS would be a perfect fit but I don’t see that will ever happen unless egos and lawyers settle things out of court regarding Java and Android. Since Java/JavaFX is open sourced I think running on iPads is more achievable.

Happy new year!

Carl

31 12 2011
dejialadejebi

Thanks Carl.

1 01 2012
dejialadejebi

Where will you recommend as the best forum out there for JavaFX Questions & Answers?

22 01 2012
tola

i think javafx is wonderful.I bought your book recently (its yet to arrive) ,but from the sample code , i noticed that you loaded videos mainly by a drag event. i have tried loading videos from a local file (on my computer) but it just doesnt work any ideas ??

22 01 2012
carldea

Deji,

Sorry for the slow reply. To find better answers to JavaFX Questions please access the Oracle’s JavaFX 2.0 forum at:
https://forums.oracle.com/forums/forum.jspa?forumID=1385

To search for existing bugs or feature requests you may want to get an account at:
http://javafx-jira.kenai.com/secure/Dashboard.jspa

-Carl

22 01 2012
carldea

Tola,

The video, must be encoded using the On2 codec in VP6. In the book to run the video example you should download the video from Media College at:http://www.mediacollege.com/adobe/flash/video/tutorial/example-flv.html.
The book mentions: “…locate the link entitled Windy 50s Mobility Scooter Race that points to our .flv media file (20051210-w50s.flv). In order to download a link consisting of a file, right-click to select “Save target as” or “Save link as”. Once you have saved the file locally on your file system, you can drag the file into the media player application to begin the demo.

If you have issues let me know what platform(Windows, Mac, etc), JavaFX, and video file format you have.

I hope you enjoy the book. Another excellent resource to look for better answers is at Oracle’s JavaFX 2.0 forums: https://forums.oracle.com/forums/forum.jspa?forumID=1385

If you are stuck on an example or there is a bug, feel free to let me know.
Thanks,
Carl

16 05 2012
John Van Camp

Sorry, but none of the code works for me, but I’m using Linux. And it is not just your book. The code from Pro JavaFX has program features that fail.

JavaFX 2.0+ is limited to Win XP SP3 or greater using the Runtime installed correctly on the Win box. We may have to wait for Java8 / JavaFX 3.x to evaluate any Java working as a cross-platform solution.

21 05 2012
carldea

John,

Do you still need help in getting the code to run?
Java 7 update 3 or later will get you the latest JavaFX 2.1.
I’ve tested and have ran most of the examples on Linux (Ubuntu) and things seemed to work fine. Please check your JAVAFX_HOME and JAVA_HOME before running the examples on the command line prompt.

-Carl

13 06 2012
JavaPins

JavaFX 2.0 Introduction by Example book…

Thank you for submitting this cool story – Trackback from JavaPins…

4 08 2012
Frans Suward (@FSuward)

Hello Carl,

My name Frans, today I become one of your twitter follower too. I already read some content of your JavaFX 2.0 Introductory by Example, and still reading.
I ‘m looking to learn more specific how JavaFX 2.0 can access MS SQL (read/update) etc. and in the other side more deeper to learn how to utilize capabilities of JavaFX 2.0 execute JavaScript/JQuery commands in their web engine environment?
Are you going to publish this kind of book article/s in the near near future? Or else can you give me hint where to look for article/s/document/book regarding these topics?
I have looked around but still can not find one yet.
Appreciate for your advice and guide.

Thanks,
Sincerely,
Frans Suward

25 08 2012
carldea

Frans,

Sorry for the slow reply.

When accessing MS SQL you might want to look at MS Support such as http://support.microsoft.com/kb/313100.
If you want to take advantage of JQuery and EXT-JS you want to use JavaFX’s WebView scene graph node. You can do amazing things by talking to the JSObject.

Hopefully I have time I’ll create some example using JQuery or EXT-JS.

Enjoy..
Thanks!
Carl

15 01 2013
Rafael

Hi, Carl! I bought your book and downloaded source code. I found that some samples doesn’t work (exception is thrown) such as CreatingImages (ch.2, sample 1), or PlayingAudio (ch.3, sample 1), and correlates.
You have idea for why ?

My system is Linux Mint release 13.

thanks in advance!

24 01 2013
carldea

Rafael,
Sorry for the slow reply.

I’ve not used Mint before. However regarding some of the examples that use undecorated windows (translucent windows) may have issues. You may want to just comment out the code section which removes the title and border areas.

Please refer to JavaFX’s Jira bug tracking or the Oracle forums for others who may be experiencing the same issue.

http://javafx-jira.kenai.com/secure/Dashboard.jspa
https://forums.oracle.com/forums/forum.jspa?forumID=1385

Take care,
Carl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 74 other followers

%d bloggers like this: