Getting Started with JavaFX 8
JavaFX is Java’s next-generation GUI toolkit. It’s a platform that makes it easy to rapidly build high-performance Java client-side applications.
JavaFX’s underlying engines take advantage of modern GPUs through hardware-accelerated graphics, while providing well-designed programming interfaces, thus enabling developers to combine graphics, animation, and UI controls.
These capabilities allow you to deliver a compelling, complex, and fully customizable client GUI for your customer that will make them quite impressed.
While the original targets of Java were the embedded and client worlds, since 2006, many reasons pushed the Java language to become the top development platform for the Enterprise world.
But recently, with the JavaFX platform’s entrance as the standard client GUI, those original targets have started to gain popularity again.
Although it is much more than just a GUI toolkit, JavaFX allows Java developers to create client applications with compelling user interfaces that easily connect to backend systems.
In addition, JavaFX’s flexible FXML support allows you to build MVC (Model-View-Controller) architectural pattern applications easily, and use the WYSIWYG approach using the Scene Builder tool.
JavaFX’s bindings feature simplified communication between entities and support MVC even further. In addition to that, JavaFX provides fast, customizable UI modeling using CSS.
By adding a full-fledged WebView component with a document model, mapping to Java code is easy and provides great support for 3D and media capabilities.
In this chapter, we are going to cover the following topics:
-
What is JavaFX and what are its targeted platforms?
-
A walk through JavaFX history
-
JavaFX goals, features, and what’s new in JavaFX 8
-
How to install Java SE 8, JavaFX 8, NetBeans, and configuring environment variables
-
Developing a "Hello World" JavaFX 8 application, and understanding the JavaFX 8 basic application architecture and building blocks
목표
JavaFX came to light with a primary goal – to be used across many types of devices, such as embedded devices, smartphones, TVs, tablet computers, and desktops. JavaFX also follows Java’s write once, run anywhere paradigm.
JavaFX 8 is written totally from scratch in Java language, it makes you feel at home. Therefore, applications written in JavaFX can be deployed on desktops, laptops, the Web, embedded systems, mobiles, and tablets.
Embedded systems are no longer supported by Oracle; it is left to companies like ARM and others to support it. Mobile devices have never been supported from JavaFX 2.x to 8.x; the support exists now only because of OpenJFX. The community has benefitted from open source bringing JavaFX to mobile environments.
For more about OpenJFX, visit https://wiki.openjdk.java.net/display/OpenJFX/Main
JavaFX is a set of graphics and a media package that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms, in one bundle, without the need for many separate libraries, frameworks, and APIs to achieve the same goal. These separate libraries include media, UI controls, WebView, 3D, and 2D APIs.
So if you are a Java frontend developer, an experienced Java Swing, Flash/Flex, SWT, or web developer looking to take your client-side applications to the next level, and you want to develop an attractive and complex user interface for your customer, then you are on track learning JavaFX skills – this book is for you.
시작
JavaFX 8을 소개한다. we have already talked about JavaFX 8 as a technology and why you should care about it.
Next, we will navigate its history, exploring its core features and where it could be used.
Before you start using this book to learn JavaFX 8, we will go through the preparation of your development environment by installing various required software bundles, to be able to compile and run many of its examples.
After installing the required software, you will begin by creating a traditional Hello JavaFX 8 example. Once you feel comfortable with the development environment, as a final verification that we are on the right track, we will walk through the Hello JavaFX 8 source code to understand the basic JavaFX 8 application architecture.
Note: If you are already familiar with the installation of the JDK and the NetBeans IDE, you can skip to Chapter 2, JavaFX 8 Essentials and Creating a custom UI, which covers JavaFX 8 fundamentals and how to create a custom UI component.
So what you are waiting for? Let’s get started!
JavaFX history
You might believe JavaFX is quite a new technology, but it actually isn’t. JavaFX has been here for a long time; unofficially since 2005. Ever since Sun Microsystems acquired the company SeeBeyond, there has been a graphics-rich scripting language known as F3 (Form Follows Function), which was created by engineer Chris Oliver.
At the JavaOne 2007 conference, Sun Microsystems officially unveiled JavaFX as the language’s name instead of F3. During the period 2007 to 2010, Oracle acquired many big companies like BEA Systems, JD Edwards, Siebel Systems, and so on. I was working for Oracle with the responsibility of integrating different customer support channels to the Oracle support website MetaLink, as it was called at that time.
On April 20, 2009, Oracle Corporation announced the acquisition of Sun Microsystems, making Oracle the new steward of JavaFX.
At JavaOne 2010, Oracle announced the JavaFX roadmap, which included its plans to phase out the JavaFX 1.3 scripting language and recreate the JavaFX platform for the Java platform as Java-based APIs. As promised, JavaFX 2.0 SDK was released at JavaOne in October 2011.
In addition to the release of JavaFX 2.0, Oracle took the platform to the next level by announcing its commitment to take steps to make JavaFX open source, thus allowing Java’s versatile and strong community to help move the platform forward. Making JavaFX open source increased its adoption, enabled a quicker turnaround time on bug fixes, and generated new enhancements.
Between the versions JavaFX 2.1 and 2.2, the number of new features grew rapidly. JavaFX 2.1 was the official release of the Java SDK on Mac OS. JavaFX 2.2 was the official release of the Java SDK on the Linux operating system.
There was no such thing as JavaFX 3.x, but the big change in the Java development world happened with the Java SE 8 release, which was announced on March 18, 2014. Java SE 8 has many new APIs and language enhancements, which include Lambdas, Stream API, Nashorn JavaScript engine, and JavaFX APIs, which are being incorporated into standard JDK bundles, and JavaFX version becomes 8 as direct successor to JavaFX 2.0.
To see all of the new features in Java SE 8, visit http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html.
When is JavaFX 8 available?
The answer is now. As mentioned before, Java SE 8 was released on March 18, 2014. For developers who use Java to build client-site applications, the JavaFX rich Internet application framework supports Java 8 now.
Most of the Java enterprise edition vendors support Java 8 too. Whether you move to Java SE 8 right away depends on the kind of projects you’re working on.
Note In fact, as outlined in the Oracle JDK Support Roadmap, after April 2015, Oracle will not post further updates of Java SE 7 to its public download sites.
The JavaFX APIs are available as a fully integrated feature of the Java SE Runtime Environment (JRE) and JDK. The JDK is available for all major desktop platforms (Windows, Mac OS X, Solaris, and Linux), therefore JavaFX will also run on all major desktop platforms.
Relating to JavaFX 8, it supports the following APIs:
-
3D graphics
-
Rich text support
-
Printing APIs.
JavaFX features
The following features are included in JavaFX 8 and later releases as per JavaFX’s official documentation:
Java APIs: JavaFX is a Java library that consists of classes and interfaces that are written in Java code.
FXML and Scene Builder: This is an XML-based declarative markup language for constructing a JavaFX application user interface. You can code in FXML or use JavaFX Scene Builder to interactively design the GUI. Scene Builder generates FXML markup that can be ported to an IDE like NetBeans, where you can add the business logic. Moreover, the FXML file that is generated can be used directly inside the JavaFX application.
WebView: This is a web component that uses WebKit, an HTML render engine technology, to make it possible to embed web pages within a JavaFX application. JavaScript running in WebView can call Java APIs and vice-versa.
Swing/SWT interoperability: The existing Swing and SWT applications can benefit from JavaFX features such as rich graphics, media playback, and embedded web content.
Built-in UI controls and CSS: JavaFX provides all the major UI controls, and some extra uncommon controls like charts, pagination, and accordion that are required to develop a full-featured application. Components can be skinned with standard web technologies such as CSS. 3D graphics features: Support for the 3D graphics library is included. Canvas API: You can draw directly inside a JavaFX scene area using the Canvas API, which consists of one graphical element (node). Multitouch support: Multitouch operations are supported based on the capabilities of the underlying platform.
Hardware-accelerated graphics pipeline: JavaFX graphics are based on the graphics-rendering pipeline, Prism. The Prism engine smoothly and quickly renders JavaFX graphics when used with a supported graphics card GPU. If a system does not feature one of them, then Prism defaults to the software-rendering stack.
High-performance media engine: This engine provides a stable, low-latency media framework that is based on the GStreamer multimedia framework. The playback of web multimedia content is supported with the media pipeline. Self-contained deployment model: Self-contained application packages have all of the application resources and a private copy of the Java and JavaFX runtimes. They are distributed as native installable packages and provide the same installation and launch experience as native applications for that OS.
What’s new in JavaFX 8
The following is a brief summary of the new features and significant product changes made in the JavaFX component of the Java SE 8 release:
The new Modena theme is now the default theme for JavaFX applications. Support for additional HTML5 features, including Web Sockets, Web Workers, Web Fonts, and printing capabilities have been added. The API enables you to embed Swing content into JavaFX applications with the new SwingNode class, which improves the Swing interoperability feature. DatePicker, Spinner, and TableView built-in UI controls are now available. It provides the public JavaFX printing APIs through the javafx.print package. Support for Hi-DPI displays has been made available. CSS-styleable classes became public APIs. A scheduled service class has been introduced. The 3D graphics library has been enhanced with several new API classes. Major updates have been added to the Camera API class in this release. Now JavaFX 8 supports rich text capabilities. These include bidirectional and complex text scripts such as Thai and Hindi in UI controls, and multiline, multistyle text in text nodes. Dialogs and accessibility APIs are supported.
In Appendix, Become a JavaFX Guru, I have provided a list of all the references (links, books, magazines, articles, blogs, and tools) and real JavaFX 8 production applications you will need to become a JavaFX guru.
The following figure shows the Ensemble8.jar application built using JavaFX 8, showing examples dealing with various JavaFX 8 components, topics and concepts. More interestingly, the source code is available to learn from and modify – consult the last chapter to see how to install this application.