Apress.Pro.JavaFX.2.pdf

(37217 KB) Pobierz
825033578.013.png
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
825033578.014.png 825033578.015.png 825033578.016.png 825033578.001.png 825033578.002.png 825033578.003.png 825033578.004.png 825033578.005.png
Contents at a Glance
Foreword .................................................................................................................. xv
About the Authors.................................................................................................... xvi
About the Technical Reviewer ............................................................................... xviii
Acknowledgments ................................................................................................... xix
Chapter 1: Getting a Jump Start in JavaFX ............................................................. 1
Chapter 2: Creating a User Interface in JavaFX..................................................... 35
Chapter 3: Properties and Bindings....................................................................... 93
Chapter 4: Building Dynamic UI Layouts in JavaFX............................................. 137
Chapter 5: Using the JavaFX UI Controls ............................................................. 183
Chapter 6: Collections and Concurrency ............................................................. 231
Chapter 7: Creating Charts in JavaFX.................................................................. 307
Chapter 8: Using the Media Classes .................................................................... 335
Chapter 9: Accessing Web Services .................................................................... 391
Chapter 10: JavaFX Languages and Markup ....................................................... 431
Appendix: The Visage Language in Depth............................................................ 477
Index ....................................................................................................................... 609
iii
825033578.006.png 825033578.007.png 825033578.008.png 825033578.009.png 825033578.010.png
C H A P T E R 1
Getting a Jump Start in JavaFX
Don’t ask what the world needs. Ask what makes you come alive, and go do it. Because
what the world needs is people who have come alive.
—Howard Thurman
At the annual JavaOne conference in May 2007, Sun Microsystems announced a new product family
named JavaFX. Its stated purpose includes enabling the development and deployment of content-rich
applications on consumer devices such as cell phones, televisions, in-dash car systems, and browsers.
Josh Marinacci, a software engineer at Sun, made the following statement very appropriately in a recent
Java Posse interview: “JavaFX is sort of a code word for reinventing client Java and fixing the sins of the
past.” Josh was referring to the fact that Java Swing and Java 2D have lots of capability, but are also very
complex. JavaFX allows us to simply and elegantly express user interfaces (UIs) with a declarative
programming style. It also leverages the full power of Java, because you can instantiate and use the
millions of Java classes that exist today. Add features such as binding the UI to properties in a model and
change listeners that reduce the need for setter methods, and you have a combination that will help
restore Java to the client side of the RIA equation.
In this chapter, we give you a jump start in developing JavaFX applications. After bringing you up to
date on the brief history of JavaFX, we show you how to get the JavaFX software development kit (SDK).
We also explore some great JavaFX resources and walk you through the process of compiling and
running JavaFX applications. In the process you’ll learn a lot about the JavaFX API as we walk through
application code together. First, however, we point out a related technology that is enabling the rise of
rich-client Java.
JavaFX Can’t Bring Rich-Client Java Back by Itself
When Java was first introduced in 1995, the hope was that the Java Runtime Environment (JRE) would
become the common client platform on which the UI portion of client–server applications could be
deployed. Although the JRE became ubiquitous on the server side of the equation, factors such as the
browser wars of the late 1990s delayed the prospect of achieving a consistent JRE on client machines.
The result has been that web browser technologies such as HTML and JavaScript have stepped in to fill
the gap, which we feel has proven suboptimal at best. The software development industry and the users
we serve need to have the JRE on all client machines so that we can break free from browser
technologies and enable graphically rich, fast-performing applications. Fortunately, the technology
known as Java SE 6 Update 10 is solving that problem.
1
825033578.011.png
CHAPTER 1 GETTING A JUMP START IN JAVAFX
Note What has come to be known as Java SE 6 Update 10 has actually had several names. It started life as the
Consumer JRE, and then Java SE 6 Update N. Then it became known as Java SE 6 Update 10. As of this writing,
Java SE 7 has been released, but we just refer to this technology as Java SE 6 Update 10.
Java SE 6 Update 10 consists of several technologies that improve the user experience related to
installing the JRE, and to deploying and running rich-client Java (and JavaFX) programs:
Java Kernel Online Installer —The JRE is now divided into small bundles. If the
user’s machine doesn’t have the JRE installed when a Java program is invoked, the
online installer will ascertain which of the bundles are needed to run the program.
Those bundles will be installed first and the program will begin executing as soon
as this takes place.
Java Auto-Updater: This provides a faster and more reliable process for updating
the JRE by using a patch-in-place mechanism.
Java Quick Starter: After a cold boot of the system, portions of the JRE are
prefetched into memory. This enables a Java program to start more quickly.
Pack200 Format: Pack200 is a highly compressed format that enables Java libraries
and resources, for example, to download more quickly than traditional JAR files.
Java Deployment Toolkit: This includes a simple JavaScript interface with which to
deploy Java applets and applications. The JavaScript library is located at a well-
known URL, and is engineered to make the right deployment decisions based on
the detected JRE environment on the user’s machine.
Next Generation Java Plug-In: This Java plug-in is much more reliable and
versatile than its predecessor. For example, you now have the ability to specify
large heap sizes, and per-applet command-line arguments. Also, it has built-in
Java Network Launching Protocol (JNLP) support as well as improved
Java/JavaScript communications.
Hardware Acceleration Support: In a media-rich environment, it is crucial to take
advantage of the graphics capabilities on the underlying hardware. For example,
Java SE 6 Update 10 currently has a hardware accelerated graphics pipeline based
on the Microsoft Direct3D API. This is a predecessor to the new Prism pipeline
that JavaFX uses.
The net result is that we are now at a point in software development history when two technologies
(JavaFX and Java SE 6 Update 10) are working together to restore rich client Java. We feel that sanity is in
the process of being restored to Internet software development, and we want you to join us in this RIA
revolution. But first, a brief history lesson about JavaFX.
A Brief History of JavaFX
JavaFX started life as the brainchild of Chris Oliver when he worked for a company named SeeBeyond.
They had the need for richer user interfaces, so Chris created a language that he dubbed F3 (Form
2
825033578.012.png
 
Zgłoś jeśli naruszono regulamin