Written by Stephen Chin
Client technologies are the basis for building any interface that users interact with. Because they are the first part of an application that a user sees, they also leave the greatest impact on your audience. Therefore, it is important that user interfaces look good and also are easy to use and intuitive.
Whether it be desktop, mobile, tablet, or embedded devices, Java client technologies provide a simple and elegant solution to building modern user experiences. Because the Java language is cross-platform, this reduces the effort to build and maintain your application for multiple screens and form factors. Also, as one of the most widely used programming languages, anyone can help maintain your code making it a solid foundation for the future.
In this chapter, we will show some examples of Java client technology in action and guide you through building your own cross-platform client to demonstrate how easy it is to accomplish this.
Java Client Technology in Action
Java client technology has been used for decades for all sorts of applications from business applications to development tools and even games. Also, now that Java runs on mobile and embedded platforms, you also find Java applications on your phone, tablet, and Raspberry Pi devices. It is often hard to tell if you are using a Java application because it is packaged together with the required Java libraries, so it appears just like any other native application.
We will explore several different Java client applications that you may or may not have used to give you an idea of the potential of this technology.
Java Client in Business
Java client technology is a staple in enterprise companies for internal applications. This is because it is great at building highly customized applications with complex controls like graphs, trees, tables, and Gantt charts. By building applications once and taking advantage of Java’s cross-platform capabilities, enterprises save on initial implementation cost as well as maintenance.
Figure 1-1 shows one of the more complex user interface screens from MINT TRMS. It makes use of trees, tables, ribbons, and Gantt charts implemented using JavaFX. This demonstrates a very complex view that would be challenging to implement in any other cross-platform technology.
To find out more about how you can easily build complex applications using pre-built JavaFX controls, check out Chapter 4, “JavaFX Controls Deep Dive.”
Gaming and 3D
Much of the success of Minecraft is through the large modding community who builds plugins that change the behavior and enhance the game taking it far beyond the original gameplay limitations. An example of a customer Minecraft server created by a young developer is shown in Figure 1-2. Java offers a great platform for building extensible applications through dynamic class loading and a secure sandbox model. Also with 10 million Java developers worldwide,6 there is no shortage of development expertise and talent.
Minecraft is built entirely in Java using client technologies such as Swing and Java 2D and a Java gaming library called LWJGL. The high level of abstraction offered by Java and these libraries made it possible for Notch to develop Minecraft in a short period of time and support a variety of platforms without a large team of developers.
An even easier 3D library to get started with is the built-in 3D support in JavaFX. You can find out more about 3D graphics in Chapter 8, “JavaFX 3D.”
Mobile Conference Apps
Java client technology is not just for the desktop. Using mobile JavaFX technology developed by Gluon,7 you can run your Java client on phones, tablets, and embedded devices like Raspberry Pi. Existing JavaFX applications can be ported directly to mobile devices with small changes in styling of controls to make them work on different screen sizes. For handling mobile-specific APIs, Gluon offers Charm Down, which provides cross-platform integration with hardware features.
Figure 1-3 shows several of the different screens within the conference application to select an event, showcase speakers, and navigate to the venue. According to Stephan Janssen, founder of the Devoxx conference family, “JavaFX Mobile technology has helped us to streamline multiple native applications into a single cross-platform application that is well supported on iOS and Android devices. This is a better experience for conference attendees and much easier to keep up-to-date.”
We have a simple mobile example coming up later in this chapter to show how easy it is to use this technology and a more thorough guide in Chapter 11, “Native Mobile Apps for iOS and Android.”
A Modern Approach to Client Java
While client Java technology has been around for a long time, the development ecosystem has been under constant change. There have been significant advances in mobile, cloud computing, and app distribution that affect how you build and distribute your client applications. This book is focused on making you successful as a modern application developer by guiding you toward design and implementation best practices.
- 1.
Target mobile first.
- 2.
Build for the cloud.
- 3.
Package your platform.
Target Mobile First
Smartphones have gotten to the point where they have the processing capability, memory, storage, and resolution to run full applications that were traditionally thought of as desktop-only. A tablet with a Bluetooth keyboard can easily be used as a desktop replacement for many use cases. Also, smartphones and tablets come with built-in Internet, which makes it possible to use them even where broadband is not available.
As discussed earlier, JavaFX has great mobile capabilities that are augmented by Gluon, who is an OpenJDK contributor. By using JavaFX mobile, you can write an application code base once and then target multiple screens including smartphones, tablets, and desktops. This gives your application a huge competitive advantage to desktop-only applications that do not allow users to take their work on the road with them. Find out more in Chapter 11, “Native Mobile Apps for iOS and Android”!
Build for the Cloud
The model for application backends has shifted from on-premise to the cloud. The reason for this is that there is an end user shift in expectations on how they interact with data. Historically, users would own and manage their data locally. With the rise of readily available high-speed connections, accessible encryption and security, and multiple screens per user, this expectation has changed. Now users expect data that is always online and available so it can be used from any device and easily shared and collaborated on.
The eteoBoard application uses SynchronizeFX12 for real-time synchronization of the user interface state between multiple clients. All of the project data gets loaded to and stored from either Atlassian Jira or Microsoft Team Foundation Server, both of which are cloud-based agile lifecycle management packages with REST interfaces. From an end user standpoint, all of this is transparent, and they get an always up-to-date view of the current project data so they can focus on the progress of the team.
This demonstrates the user expectation shift that data is always online and available, which has necessitated the need for client applications to tightly integrate with cloud backends. For more information on how you can take advantage of the cloud in your client applications, check out the section “Building for the Cloud” in Chapter 9, “JavaFX, the Web, and Cloud Infrastructure.”
Package Your Platform
Legacy technologies like Applets and Web Start are not recommended as a way to distribute your application. Since they rely on a shared Java runtime, a misconfigured system can easily result in the failure of an end user to run your application. Worse yet, these technologies introduce a security issue if not kept up-to-date. As a result, these are deprecated and should not be used.14
Instead, you should package everything your application needs as a single distribution. This includes the class files, libraries, and even the Java runtime required to run your application. While this may seem like a lot to include, it only costs an extra 20–30 MB on average and guarantees that your users will have a working Java runtime that is tested with the specific version of the application you are using.
Java 13 will reintroduce a tool called jpackager that takes care of bundling the Java runtime together with your application for distribution. You can find out more about this and other packaging solutions in Chapter 10, “Packaging Apps for the Desktop.” In addition, Chapter 11, “Native Mobile Apps for iOS and Android,” builds on this with information on packaging your mobile apps and releasing them in the app stores on iOS and Android devices.
Setting Up Your Environment
To get started with client programming, we are going to do a small sample application using JavaFX technology. To do this, we need to have a modern Java version as well as the JavaFX libraries. Our recommendation is to always go with the most recent version of OpenJDK, because it provides the latest security patches and is supported for free by Oracle. Also, if you are following the best practice of packaging your application, it doesn’t matter what version of Java your end user has installed because you will be bundling the latest Java runtime with your application.
OpenJDK does not come with an installer, but it is easy enough to install it from the command line. Here are the instructions per operating system on how to do this easily.
Mac OS X JDK Installation
Again, substitute the right folder name for the version of OpenJDK you unzipped and enter your admin password since this is a protected folder.
Windows JDK Installation
Windows JDK comes as a zip file. To install it, unzip it to a suitable file location, such as C:/Program Files/Java/. If you have not previously installed a JDK, this folder may not exist, but can be created with admin privileges. The copy operation to this folder will also require admin privileges, which Windows should prompt you to confirm.
In the Environment Variables dialog, we will create one new variable and modify the Path. To create a new variable, click the “New…” button under “System variables.” Call this variable “JAVA_HOME” and give it a value that is the location of your newly unzipped OpenJDK distribution. To avoid mistyping, you can use the “Browse Directory…” button to select the folder you created earlier. The exact value will vary based on your JDK version, but mine was “C:Program FilesJavajdk-12.0.1.”
Click the “New” button and enter in “%JAVA_HOME%in” as the value. If you are on an earlier version of Windows, you may just have a simple text field with individual path elements separated by semicolons. If this is the case, just go to the end of the field and enter in the JDK bin value preceded by a semicolon. If you have a previous installation of Java, you may need to find the Path entry for that and delete it so it doesn’t override your new install. Once completed, click “OK” and exit the dialogs.
Linux JDK Installation
If you are on Linux, installing OpenJDK is a breeze. Most Linux distributions come with OpenJDK preinstalled, and it can be easily updated by running the appropriate command for your package managed.
Ubuntu, Debian: sudo apt-get install openjdk-12-jdk
Red Hat, Oracle Linux: sudo yum install java-12-openjdk-devel
If your newly installed Java distribution is not picked up as the default, you can use the update-alternatives command to modify the default Java version and add in your new OpenJDK distribution if it does not show up in the list.
JavaFX Installation
Choose the matching version of JavaFX for the OpenJDK version you installed earlier, which is JavaFX 12 as of the time of writing. You may need to scroll down past the “Long Term Support” builds section and find the “Latest Release” farther down on the page. Download the SDK for your platform of choice.
Once downloaded, unzip the JavaFX SDK to a directory of your choice. For the purpose of this book, we will assume you left it in your Downloads folder, but feel free to move it to a more permanent location.
Now your Java and JavaFX installation is all prepared, and you are ready to create a modern client application. We will walk you through the steps for your first client application in the next section.
Your First Modern Java Client
We are going to guide you through creating your first client application using JavaFX technology, which is the most modern user interface platform available. There is a rich set of tools for Java development also building UIs with JavaFX, so for this first tutorial you won’t even need to write a single line of code. However, this will give you an appreciation for what is possible with modern client technology and lay a foundation for UI concepts introduced in subsequent chapters.
Coding Client Apps with IntelliJ IDEA
It is possible to code JavaFX applications with any IDE of your choice, including IntelliJ, NetBeans, Eclipse, or Visual Code. However, we are going to introduce client development with IntelliJ Community Edition, which is the industry standard for coding in Java and entirely free for client development.
The standard project creates a minimal JavaFX application that contains a Main class, a Controller, and an FXML file for the user interface. This project is not runnable yet since it cannot find the JavaFX libraries that we downloaded earlier, which is obvious from the red highlighting. To fix this, we need to add a module dependency on the JavaFX libraries.
To add the JavaFX libraries, click the “+” symbol at the bottom of the pane and select “JARs or directories…” Then navigate to the OpenJFX JDK you downloaded and unzipped earlier and choose the “lib” folder. Click “OK,” and this will complete the dependency, fixing the syntax highlighting.
To add in the missing runtime components, open the Configuration dialog by going to the “Run” menu and choosing “Edit Configurations…” This will open the dialog shown in Figure 1-17 where you can enter in VM options that get passed to the Java Virtual Machine.
Be sure to modify the module path to the correct location for your user (it must be fully qualified), the right version of the JavaFX SDK, and the platform-specific path separators. An easy way to make sure you get this right is to right-click the field and choose “Insert Path” so you get a standard file system picker and IntelliJ will create the right path format for your operating system.
Also note that we have only specified the javafx.controls and javafx.fxml modules. If your application requires additional modules, be sure to specify them in this comma-separated list. However, you often can exclude modules if they are automatically included by an inter-module dependency, such as javafx.base , that all the other modules depend upon.
In the next section, we will show you how to modify this application to build a quick Hello Modern World application.
Rapid Application Development with Scene Builder
Scene Builder is a great tool to rapidly build modern client applications. It gives a WYSIWYG (what you see is what you get) visual representation of your application as you are building it with convenient palettes for layouts and controls and a properties editor for components you add to the scene graph. It also directly operates on FXML files as the intermediate format to declaratively specify your user interface without impacting your Java code.
The IntelliJ template already includes the boilerplate code to create a new application off of an FXML file, so we just need to edit the FXML file in Scene Builder to modify the user interface.
After installing and running Scene Builder, you will be presented with a welcome dialog where you can open an existing project. Navigate to your HelloModernWorld project folder, and choose the sample.fxml file that is in the src/sample directory.
Place this file in the same folder as the FXML file and then select it in Scene Builder by updating the Image property on the right pane. To bring up the File Selection dialog, click the “…” button next to the text entry field.
Next, click “Containers” on the left and drag an AnchorPane as a child of the GridPane on the bottom-left “Hierarchy” pane. This will allow us to add additional controls that we can drag around Scene Builder and position freely.
Hello World: Font, Courier; Size, 96 px; Color, WHITE
Java: Font, System; Size, 77 px; Color, #FFA518
FX: Font, System; Size, 77 px; Color, #5382A1
And finally, to make the text pop out a bit, you can add a visual effect. Select the AnchorPane element in the “Hierarchy” pane and go to the “Modify” menu and choose the “Set Effect” submenu. In there are a selection of different effects you can apply to any element in the scene graph. Choose the “Bloom” effect, and you will get a distinct visual style.
Save your changes to the FXML file by going to the “File” menu and choosing “Save.” This will automatically update the file in your project and should allow you to immediately run your application and see the changes.
Delete the size constraints on the Scene. Simply erase the second and third parameters that specify a fixed size in the constructor.
Change the window title. Simply update the setTitle call to name the window “Hello Modern World” to match the project name.
Main class for Hello Modern World
The Path to Modern Client Development
This chapter set the groundwork for understanding recent developments in client programming for Java with examples of applications used in the real world that you may not have even realized were written in this very powerful programming language. Also, you learned how you should approach building modern client applications with a mobile-first approach, building for the cloud, and packaging your application for end users. You were also able to successfully complete building your first modern client application in JavaFX, which is the latest in Java client frameworks.
In the coming chapters, we are going to cover these topics in more detail, expanding on how you can go about packaging your application for multiple platforms, architecting your application for integration with REST and other cloud architectures, and showcasing the latest capabilities of Java client frameworks. We hope you are as eager to learn how to build modern UIs as we are to write and share this knowledge.