Pro JavaFX 9: A Definitive Guide to Building Desktop, Mobile, and Embedded Java Clients
By Johan Vos, Stephen Chin, Weiqi Gao and
()
About this ebook
Use the JavaFX platform to create rich-client Java applications and discover how you can use this powerful Java-based UI platform, which is capable of handling large-scale data-driven business applications for PC as well as mobile and embedded devices. The expert authors cover the new more modular JavaFX 9 APIs, development tools, and best practices and provide code examples that explore the exciting new features provided with JavaFX 9, part of Oracle's new Java 9 release. Pro JavaFX 9: A Definitive Guide to Building Desktop, Mobile, and Embedded Java Clients also contains engaging tutorials that cover virtually every facet of JavaFX development and reference materials on JavaFX that augment the JavaFX API documentation.
What You'll Learn
- Create a user interface in JavaFX
- Use SceneBuilder to create a user interface
- Build dynamic UI layouts in JavaFX and using the JavaFX UI controls
- Create charts in JavaFX
- Leverage JavaFX languages and markup
Who This Book Is For
Experienced Java programmers looking to learn and leverage JavaFX 9 for rich client-side Java development.
Read more from Johan Vos
Quantum Computing in Action Rating: 0 out of 5 stars0 ratingsThe Definitive Guide to Modern Java Clients with JavaFX: Cross-Platform Mobile and Cloud Development Rating: 0 out of 5 stars0 ratingsThe Definitive Guide to Modern Java Clients with JavaFX 17: Cross-Platform Mobile and Cloud Development Rating: 0 out of 5 stars0 ratings
Related to Pro JavaFX 9
Related ebooks
OCP Oracle Certified Professional Java SE 17 Developer Practice Tests: Exam 1Z0-829 Rating: 5 out of 5 stars5/5Professional XML Development with Apache Tools: Xerces, Xalan, FOP, Cocoon, Axis, Xindice Rating: 3 out of 5 stars3/5OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z0-809 Rating: 5 out of 5 stars5/5Ansible Playbook Essentials Rating: 0 out of 5 stars0 ratingsPractical Oracle JET: Developing Enterprise Applications in JavaScript Rating: 0 out of 5 stars0 ratingsVMware vSphere Performance: Designing CPU, Memory, Storage, and Networking for Performance-Intensive Workloads Rating: 0 out of 5 stars0 ratingsIntroduction to JVM Languages Rating: 0 out of 5 stars0 ratingsJava All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsBeginning JSON Rating: 0 out of 5 stars0 ratingsPractical WebDriverIO: Learn to Automate Effectively Using WebDriverIO APIs Rating: 0 out of 5 stars0 ratingsProfessional XMPP Programming with JavaScript and jQuery Rating: 3 out of 5 stars3/5OCA / OCP Java SE 8 Programmer Practice Tests Rating: 5 out of 5 stars5/5Interprocess Communication with macOS: Apple IPC Methods Rating: 0 out of 5 stars0 ratingsOCP Oracle Certified Professional Java SE 11 Developer Practice Tests: Exam 1Z0-819 and Upgrade Exam 1Z0-817 Rating: 5 out of 5 stars5/5Beginning PHP5, Apache, and MySQL Web Development Rating: 0 out of 5 stars0 ratingsMastering Java EE Development with WildFly Rating: 0 out of 5 stars0 ratingsModern Full-Stack Development: Using TypeScript, React, Node.js, Webpack, and Docker Rating: 0 out of 5 stars0 ratingsMastering Reactive JavaScript Rating: 0 out of 5 stars0 ratingsBuilding Websites with e107 Rating: 0 out of 5 stars0 ratingsReliable JavaScript: How to Code Safely in the World's Most Dangerous Language Rating: 0 out of 5 stars0 ratingsWindows Server 2008 Hyper-V: Insiders Guide to Microsoft's Hypervisor Rating: 0 out of 5 stars0 ratingsScala Test-Driven Development Rating: 0 out of 5 stars0 ratingsJava EE 7 Recipes: A Problem-Solution Approach Rating: 0 out of 5 stars0 ratingsiOS Application Development with OpenCV 3 Rating: 0 out of 5 stars0 ratingsBackbone.js Testing Rating: 0 out of 5 stars0 ratingsCode with Java 21: A practical approach for building robust and efficient applications (English Edition) Rating: 0 out of 5 stars0 ratingsMastering VMware Infrastructure 3 Rating: 3 out of 5 stars3/5Clean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5PHP5 and MySQL Bible Rating: 3 out of 5 stars3/5NW.js Essentials Rating: 0 out of 5 stars0 ratings
Programming For You
Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Automate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsC# 7.0 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Problem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5
Reviews for Pro JavaFX 9
0 ratings0 reviews
Book preview
Pro JavaFX 9 - Johan Vos
© Johan Vos, Stephen Chin, Weiqi Gao, James Weaver, and Dean Iverson 2018
Johan Vos, Stephen Chin, Weiqi Gao, James Weaver and Dean IversonPro JavaFX 9https://doi.org/10.1007/978-1-4842-3042-8_1
1. Getting a Jump-Start in JavaFX
Johan Vos¹ , Stephen Chin², Weiqi Gao³, James Weaver⁴ and Dean Iverson⁵
(1)
Leuven, Belgium
(2)
BELMONT, California, USA
(3)
Ballwin, Missouri, USA
(4)
Marion, Indiana, USA
(5)
Fort Collins, Colorado, USA
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 Java Posse interview: JavaFX is sort of a code word for reinventing client Java and fixing the sins of the past.
He was referring to the fact that Java Swing and Java 2D have lots of capability, but are also very complex. Furthermore, technologies have evolved a lot since Swing and Java 2D were created. Today’s client systems (desktops as well as mobile and embedded devices) are equipped with powerful graphical processors—the GPU. JavaFX takes advantage of the new features and performance increases offered by GPUs. By using FXML , 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 Internet applications.
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 required tools. 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 application programming interface (API) as we walk through application code together.
A Brief History of JavaFX
JavaFX started life as the brainchild of Chris Oliver when he worked for a company named SeeBeyond. They had a need for richer user interfaces, so Chris created a language that he dubbed F3 (Form Follows Function) for that purpose. In the article Mind-Bendingly Cool Innovation
(cited in the Resources
section at the end of this chapter), Chris is quoted as follows: When it comes to integrating people into business processes, you need graphical user interfaces for them to interact with, so there was a use case for graphics in the enterprise application space, and there was an interest at SeeBeyond in having richer user interfaces.
SeeBeyond was acquired by Sun, who subsequently changed the name of F3 to JavaFX, and announced it at JavaOne 2007. Chris Oliver joined Sun during the acquisition and continued to lead the development of JavaFX.
The first version of JavaFX Script was an interpreted language, and was considered a prototype of the compiled JavaFX Script language that was to come later. Interpreted JavaFX Script was very robust, and there were two JavaFX books published in the latter part of 2007 based on that version. One was written in Japanese, and the other was written in English (JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-Side Applications by Jim Weaver (Apress, 2007)).
While developers were experimenting with JavaFX and providing feedback for improvement, the JavaFX Script compiler team at Sun was busy creating a compiled version of the language. This included a new set of runtime API libraries. The JavaFX Script compiler project reached a tipping point in early December 2007, which was commemorated in a blog post entitled Congratulations to the JavaFX Script Compiler Team—The Elephant Is Through the Door.
That phrase came from the JavaFX Script compiler project leader Tom Ball in a blog post, which contained the following excerpt.
An elephant analogy came to me when I was recently grilled about exactly when the JavaFX Script compiler team will deliver our first milestone release. I can’t give you an accurate date,
I said. It’s like pushing an elephant through a door; until a critical mass makes it past the threshold you just don’t know when you’ll be finished. Once you pass that threshold, though, the rest happens quickly and in a manner that can be more accurately predicted.
A screenshot of the silly, compiled JavaFX application written by one of the authors, Jim Weaver, for that post is shown in Figure 1-1, demonstrating that the project had in fact reached the critical mass to which Tom Ball referred.
../images/323806_4_En_1_Chapter/323806_4_En_1_Fig1_HTML.jpgFigure 1-1.
Screenshot for the Elephant Is Through the Door
program
Much progress continued to be made on JavaFX in 2008:
The NetBeans JavaFX plug-in became available for the compiled version in March 2008.
Many of the JavaFX runtime libraries (mostly focusing on the UI aspects of JavaFX) were rewritten by a team that included some very talented developers from the Java Swing team.
In July 2008, the JavaFX Preview Software Development Kit (SDK) was released, and at JavaOne 2008, Sun announced that the JavaFX 1.0 SDK would be released in fall 2008.
On December 4, 2008, the JavaFX 1.0 SDK was released. This event increased the adoption rate of JavaFX by developers and IT managers because it represented a stable codebase.
In April 2009, Oracle and Sun announced that Oracle would be acquiring Sun. The JavaFX 1.2 SDK was released at JavaOne 2009.
In January 2010, Oracle completed its acquisition of Sun. The JavaFX 1.3 SDK was released in April 2010, with JavaFX 1.3.1 being the last of the 1.3 releases.
At JavaOne 2010, JavaFX 2.0 was announced. The JavaFX 2.0 roadmap was published by Oracle and included items such as the following.
Deprecate the JavaFX Script language in favor of using Java and the JavaFX 2.0 API. This brings JavaFX into the mainstream by making it available to any language (e.g., Java, Groovy, and JRuby) that runs on the Java Virtual Machine (JVM) . As a consequence, existing developers do not need to learn a new language, but they can use existing skills and start developing JavaFX applications.
Make the compelling features of JavaFX Script, including binding to expressions, available in the JavaFX 2.0 API .
Offer an increasingly rich set of UI components, building on the components already available in JavaFX 1.3.
Provide a Web component for embedding HTML and JavaScript content into JavaFX applications.
Enable JavaFX interoperability with Swing.
Rewrite the media stack from the ground up.
JavaFX 2.0 was released at JavaOne 2011, and has enjoyed a greatly increased adoption rate due to the innovative features articulated previously.
JavaFX 8 marked another important milestone. JavaFX is now an integral part of the Java Platform, Standard Edition.
This is a clear indication that JavaFX is considered mature enough, and that it is the future of Java on the client.
This greatly benefits developers, as they don’t have to download two SDKs and tool suites.
The new technologies in Java 8, in particular the lambda expressions, Stream API, and default interface methods, are very usable in JavaFX.
Many new features have been added, including native 3D support, a printing API, and some new controls including a datepicker.
Since the release of JavaFX 8, the JavaFX platform follows the same version and release procedures as the Java Platform, Standard Edition. As a consequence, when Java 9 was released, JavaFX 9 was released as well.
The main focus for Java 9 is modularity. The Java Platform, Standard Edition, has become bigger and bigger, and not all applications require all classes to be available. By modularizing the Java Platform, it is easier to create subsets of the Java platform that combine a number of modules that are sufficient to run a particular application. This modularization effort was huge, and it took many years before it was complete. All parts of the Java Platform, Standard Edition have been refactored into modules, including the JavaFX 9 Platform APIs.
One of the consequences of the modularization is that it is now not allowed anymore for code to depend on internal APIs of another module. This has far-reaching consequences. Before JavaFX 9 , Controls were often created by implementing undocumented internal APIs. Those APIs were public, because they were used internally by other JavaFX classes, in different packages. As a consequence, developer could use them as well.
Since those internal APIs are now in modules that by default do not expose this functionality, a new approach was needed for developers who want to create custom controls. Hence, the JavaFX team was not only faced with moving all the JavaFX public APIs into a number of modules, it also had to provide public APIs for functionality that was previously accessed via internal APIs.
In Java 9 , the JavaFX platform provides the following modules:
javafx.base
javafx.controls
javafx.fxml
javafx.graphics
javafx.jmx
javafx.media
javafx.swing
javafx.swt
javafx.web
jdk.packager
jdk.packager.services
Now that you’ve had the obligatory history lesson in JavaFX, let’s get one step closer to writing code by showing you where some examples, tools, and other resources are.
Prepare Your JavaFX Journey
Required Tools
Because JavaFX is part of Java 9, you don’t have to download a separate JavaFX SDK . The whole JavaFX API and implementation is part of the Java 9 SE SDK that can be downloaded from www.oracle.com/technetwork/java/javase/downloads/index.html .
This SDK contains everything you need to develop, run, and package JavaFX applications. You can compile JavaFX applications using command-line tools contained in the Java 9 SE SDK.
Most developers, however, prefer an integrated development environment (IDE) for increased productivity. By definition, an IDE that supports Java 9 also supports JavaFX 9. Hence, you can use your favorite IDE and develop JavaFX applications. In this book, we mainly use the NetBeans IDE, but other IDE’s, such as IntelliJ or Eclipse, can be used as well. The NetBeans IDE can be downloaded from https://netbeans.org/downloads .
Many JavaFX developers, especially those working on user interfaces, prefer a WYSIWYG tool for creating interfaces. Scene Builder is a stand-alone tool that allows you to design JavaFX interfaces rather than coding them. We discuss Scene Builder in Chapter 4. Although Scene Builder produces FXML—and we discuss FXML in Chapter 3 as well—that can be used in any IDE, NetBeans provides a tight integration with Scene Builder. The Scene Builder tool can be downloaded at http://gluonhq.com/products/scene-builder/ .
JavaFX, the Community
JavaFX is not a closed-source project, developed in a secret bunker. To the contrary, JavaFX is being developed in an open spirit, with an open source code base, open mailing lists, and an open and active community sharing knowledge.
The source code is developed in the OpenJFX project, which is a subproject of the OpenJDK project in which Java SE is being developed. If you want to examine the source code or the architecture, or if you want to read the technical discussions on the mailing list, have a look at http://openjdk.java.net/projects/openjfx .
The developer community is very active, both in OpenJFX as well as in application-specific areas. Many JavaFX developers regularly blog about their JavaFX activities, and many non-Oracle products and projects related to JavaFX are being created and maintained by this community.
In addition, blogs maintained by JavaFX engineers and developers are great resources for up-to-the-minute technical information on JavaFX. For example, Oracle JavaFX Engineer Jonathan Giles keep the developer community apprised of the latest JavaFX innovations at http://fxexperience.com . The Resources
section at the end of this chapter contains the URLs of the blogs that the authors of this book use to engage the JavaFX developer community.
Two important characteristics of the JavaFX Community are its own creativity and the desire to share. There are a number of open-source efforts bringing added value to the JavaFX Platform. Because of good cooperation between the JavaFX platform engineers and the external JavaFX developers, these open-source projects fit very well with the official JavaFX platform.
Some of the most interesting efforts are listed here:
Gluon allows you to create iOS and Android applications using Java and JavaFX. As a consequence, your JavaFX application can be used to create an app for Android devices and for the iPhone or the iPad.
This mobile port of JavaFX is discussed in more detail in Chapter 12.
ControlsFX is a project working on adding high-quality controls and add-ons to the JavaFX platform.
JFXtras.org is another project working on adding high-quality controls and add-ons to the JavaFX platform.
It is worth mentioning that the JavaFX team is closely watching the efforts in both JFXtras.org and ControlsFX, and ideas that start in one of those projects might make it into one of the next releases of JavaFX.
Take a few minutes to explore these sites. Next, we point out some valuable resources.
Use the Official Specifications
While developing JavaFX applications, it is very useful to have access to the API Javadoc documentation, which is available at http://download.java.net/jdk9/jfxdocs/index.html and shown in Figure 1-2.
../images/323806_4_En_1_Chapter/323806_4_En_1_Fig2_HTML.jpgFigure 1-2.
JavaFX SDK API Javadoc
The API documentation in Figure 1-2, for example, shows how to use the Rectangle class, located in the javafx.scene.shape package. Scrolling down this web page shows the properties, constructors, methods, and other helpful information about the Rectangle class. By the way, this API documentation is available in the Java 8 SE SDK that you downloaded, but we wanted you to know how to find it online as well.
Apart from the Javadoc, it is very useful to have the Cascading Style Sheets (CSS) style reference at hand as well. This document explains all the style classes that can be applied to a particular JavaFX element. You can find this document at http://download.java.net/jdk9/jfxdocs/javafx/scene/doc-files/cssref.html .
Scenic View
You already downloaded Scene Builder, which is the tool that allows you to create UIs by designing them, rather than writing code. We expect that there will be more tools developed by companies and individuals that help you create JavaFX applications. One of the first tools that was made available for free and that is very helpful when debugging JavaFX applications is ScenicView, originally created by Amy Fowler at Oracle, and later maintained by Jonathan Giles. You can download ScenicView at http://scenic-view.org/ .
ScenicView is particularly helpful because it provides a convenient UI that allows developers to inspect properties of nodes (i.e., dimensions, translations, CSS) at runtime.
Packaging and Distribution
The techniques used for delivering software to the end user are always changing. In the past, the preferred way for delivering Java applications was via the Java Network Launch Protocol (JNLP). Doing so, both applets and stand-alone applications can be installed on a client. However, there are a number of issues with this technique. The idea only works if the end user has a JVM installed that is capable of executing the application. This is not always true. Even in the desktop world, where a system can be delivered preinstalled with a JVM, there are issues with versioning and security. Indeed, some applications are hard-coded against a specific version of the JVM. Although vulnerabilities in the JVM are in most cases fixed very fast, this still requires the end user to always install the latest version of the JVM, which can be pretty frustrating.
On top of that, browser manufacturers are increasingly reluctant to support alternative embedded platforms. In summary, relying on a browser and on a local, preinstalled JVM does not provide the best end-user experience.
The client software industry is shifting more and more toward the so-called app stores. In this concept, applications can be downloaded and installed that are self-containing. They do not rely on preinstalled execution environments. The principles originated in the mobile space, where Apple’s AppStore and Android’s Play Store are leading the market. Especially in these markets, single-click installs have a huge advantage over local downloads, unpacking, manual configuration, and more nightmares.
In Java terminology, a self-contained application means that the application is bundled together with a JVM that is capable of running the application. In the past, this idea was often rejected because it made the application bundle too big. However, with increasing memory and storage capacities, and with decreasing costs of sending bytes over the Internet, this disadvantage is becoming less relevant.
There are a number of technologies being developed currently that help you bundle your application with the correct JVM version and package it.
The standard technology for bundling Java applications with a Java Virtual Machine runtime is the JavaPackager, which is developed inside the OpenJFX project area. JavaFXPackager contains an API for creating self-contained bundles. This tool is used by NetBeans, and it can be used to generate self-contained bundles with just a few clicks.
Now that you have the tools installed, we show you how to create a simple JavaFX program, and then we walk through it in detail. The first program that we’ve chosen for you is called Hello Earthrise,
which demonstrates more features than the typical beginning Hello World
program.
Developing Your First JavaFX Program: Hello Earthrise
On Christmas Eve in 1968, the crew of Apollo 8 entered lunar orbit for the first time in history. They were the first humans to witness an Earthrise,
taking the magnificent picture shown in Figure 1-3. This image is dynamically loaded from this book’s web site when the program starts, so you’ll need to be connected to the Internet to view it.
Figure 1-3.
The Hello Earthrise program
In addition to demonstrating how to dynamically load images over the Internet, this example shows you how to use animation in JavaFX. Now it’s time for you to compile and run the program. We show you two ways to do this: from the command line and using NetBeans.
Compiling and Running from the Command Line
We usually use an IDE to build and run JavaFX programs, but to take all of the mystery out of the process we use the command-line tools first.
Note
For this exercise, as with most others in the book, you need the source code. If you prefer not to type the source code into a text editor, you can obtain the source code for all of the examples in this book from the code download site. See the Resources
section at the end of this chapter for the location of this site.
Assuming that you’ve downloaded and extracted the source code for this book into a directory, follow the directions in this exercise, performing all of the steps as instructed. We dissect the source code after the exercise.
Compiling and Running the Hello Earthrise Program from the Command Line
You’ll use the javac and java command-line tools to compile and run the program in this exercise. From the command-line prompt on your machine:
1.
Navigate to the Chapter01/Hello directory.
2.
Execute the following command to compile the HelloEarthRiseMain.java file.
javac -d . HelloEarthRiseMain.java
3.
Because the –d option was used in this command, the class files generated are placed in directories matching the package statements in the source files. The roots of those directories are specified by the argument given for the –d option, in this case the current directory.
4.
To run the program, execute the following command. Note that we use the fully qualified name of the class that will be executed, which entails specifying the nodes of the path name and the name of the class, all separated by periods.
java projavafx.helloearthrise.ui.HelloEarthRiseMain
The program should appear as shown in Figure 1-4, with the text scrolling slowly upward, reminiscent of the Star Wars opening crawls .
Congratulations on completing your first exercise as you explore JavaFX!
Understanding the Hello Earthrise Program
Now that you’ve run the application, let’s walk through the program listing together. The code for the Hello Earthrise application is shown in Listing 1-1.
package projavafx.helloearthrise.ui;
import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* Main class for the Hello World
style example
*/
public class HelloEarthRiseMain extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
String message
= Earthrise at Christmas:
+ [Forty] years ago this Christmas, a turbulent world
+ looked to the heavens for a unique view of our home
+ planet. This photo of Earthrise over the lunar horizon
+ was taken by the Apollo 8 crew in December 1968, showing
+ Earth for the first time as it appears from deep space.
+ Astronauts Frank Borman, Jim Lovell and William Anders
+ had become the first humans to leave Earth orbit,
+ entering lunar orbit on Christmas Eve. In a historic live
+ broadcast that night, the crew took turns reading from
+ the Book of Genesis, closing with a holiday wish from
+ Commander Borman: \"We close with good night, good luck,
+ a Merry Christmas, and God bless all of you all of
+ you on the good Earth.\"
;
// Reference to the Text
Text textRef = new Text(message);
textRef.setLayoutY(100);
textRef.setTextOrigin(VPos.TOP);
textRef.setTextAlignment(TextAlignment.JUSTIFY);
textRef.setWrappingWidth(400);
textRef.setFill(Color.rgb(187, 195, 107));
textRef.setFont(Font.font(SansSerif
, FontWeight.BOLD, 24));
// Provides the animated scrolling behavior for the text
TranslateTransition transTransition = new TranslateTransition(new Duration(75000), textRef);
transTransition.setToY(-820);
transTransition.setInterpolator(Interpolator.LINEAR);
transTransition.setCycleCount(Timeline.INDEFINITE);
// Create an ImageView containing the Image
Image image = new Image (http://projavafx.com/images/earthrise.jpg
);
ImageView imageView = new ImageView(image);
// Create a Group containing the text
Group textGroup = new Group(textRef);
textGroup.setLayoutX(50);
textGroup.setLayoutY(180);
textGroup.setClip(new Rectangle(430, 85));
// Combine ImageView and Group
Group root = new Group(imageView, textGroup);
Scene scene = new Scene(root, 516, 387);
stage.setScene(scene);
stage.setTitle(Hello Earthrise
);
stage.show();
// Start the text animation
transTransition.play();
}
}
Listing 1-1.
The HelloEarthRiseMain.java Program
Now that you’ve seen the code, let’s take a look at its constructs and concepts in some more detail.
What Happened to the Builders?
If you were using JavaFX 2 before, you are probably familiar with the so-called builder pattern. Builders provide a declarative style of programming. Rather than calling set() methods on a class instance to specify its fields, the builder pattern uses an instance of a Builder class to define how the target class should be composed.
Builders were very popular in JavaFX . However, it turned out that there were major technical hurdles with keeping them in the platform. As a consequence, the decision was made to phase out builders. In Java 8, Builder classes were still usable, but they are deprecated. In Java 9, Builder classes have been removed entirely.
More information on the reason why Builder classes are not preferred anymore can be found in a mailing list entry by JavaFX Client Architect Richard Bair at http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-March/006725.html . The bottom of this entry contains a very important statement: I believe that FXML or lambda’s or alternative languages all provide other avenues for achieving the same goals as builders but without the additional cost in byte codes or classes.
This is what we will show throughout this book. Near the end of this chapter, we show a first example of a lambda expression in our code. In Chapter 3, we show how Scene Builder and FXML allow you to use a declarative way of defining a UI.
In the current example, we programmatically define the different components of the UI, and we glue them together. In Chapter 3, we show the same example using a declarative FXML-based approach.
The JavaFX Application
Let’s have a look at the class declaration in our first example:
public class HelloEarthRiseMain extends Application
This declaration states that our application extends the javafx.application.Application class. This class has one abstract method that we should implement:
public void start(Stage stage) {}
This method will be called by the environment that executes our JavaFX application.
Depending on the environment, JavaFX applications will be launched in a different way. As a developer, you don’t have to worry about how your application is launched, and where the connection to a physical screen is made. You have to implement the start
method and use the provided Stage parameter to create your UI, as discussed in the next paragraph.
In our command-line example, we launched the applications by executing the main method of the application class. The implementation of the main method is very simple:
public static void main(String[] args) {
Application.launch(args);
}
The only instruction in this main method is a call to the static launch method of the application, which will launch the application.
Tip
A JavaFX