ebook img

Learn JavaFX 8: Building User Experience and Interfaces with Java 8 PDF

1210 Pages·2015·18.253 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn JavaFX 8: Building User Experience and Interfaces with Java 8

BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Sharan Learn JavaFX 8 RELATED Learn JavaFX 8 shows you how to start developing rich-client desktop applications using your Java skills and provides comprehensive coverage of JavaFX 8’s features. Each chapter starts with an introduction to the topic at hand, followed by a step-by-step discussion of the topic with small snippets of code. The book contains numerous figures aiding readers in visualizing the GUI that is built at every step in the discussion. The book starts with an introduction to JavaFX and its history. It lists the system requirements and the steps to start developing JavaFX applications. It shows you how to create a Hello World application in JavaFX, explaining every line of code in the process. Later in the book, author Kishori Sharan discusses advanced topics such as 2D and 3D graphics, charts, FXML, advanced controls, and printing. Some of the advanced controls such as TableView, TreeTableView and WebView are covered at length in separate chapters. This book provides complete and comprehensive coverage of JavaFX 8 features; uses an incremental approach to teach JavaFX, assuming no prior GUI knowledge; includes code snippets, complete programs, and pictures; covers MVC patterns using JavaFX; and covers advanced topics such as FXML, effects, transformations, charts, images, canvas, audio and video, DnD, and more. So, after reading and using this book, you’ll come away with a comprehensive introduction to the JavaFX APIs as found in the new Java 8 platform. • How to develop rich-client desktop applications using JavaFX 8 • How to use properties, collections, colors, and styles • How to use controls and handle events to build modern GUI applications • How to use advanced controls such as TreeView, TableView, and TreeTableView • How to access webpages in JavaFX applications • How to draw 2D and 3D shapes, and apply effects and transformations • How to create animations and charts using the JavaFX 8 APIs • How to add audio and video to your applications • How to create GUIs in JavaFX using FXML • How to provide printing capabilities using the JavaFX Print API Shelve in ISBN 978-1-4842-1143-4 Programming Languages/Java 55999 User level: Beginning–Intermediate SOURCE CODE ONLINE 9781484211434 www.apress.com 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. Contents at a Glance About the Author ����������������������������������������������������������������������������������������������������xxi About the Technical Reviewers ����������������������������������������������������������������������������xxiii Acknowledgments �������������������������������������������������������������������������������������������������xxv Introduction ���������������������������������������������������������������������������������������������������������xxvii ■ Chapter 1: Getting Started �������������������������������������������������������������������������������������1 ■ Chapter 2: Properties and Bindings ���������������������������������������������������������������������25 ■ Chapter 3: Observable Collections�����������������������������������������������������������������������83 ■ Chapter 4: Managing Stages �����������������������������������������������������������������������������127 ■ Chapter 5: Making Scenes ���������������������������������������������������������������������������������149 ■ Chapter 6: Understanding Nodes �����������������������������������������������������������������������163 ■ Chapter 7: Playing with Colors ��������������������������������������������������������������������������201 ■ Chapter 8: Styling Nodes �����������������������������������������������������������������������������������223 ■ Chapter 9: Event Handling ���������������������������������������������������������������������������������259 ■ Chapter 10: Understanding Layout Panes ����������������������������������������������������������303 ■ Chapter 11: Model-View-Controller Pattern ������������������������������������������������������419 ■ Chapter 12: Understanding Controls �����������������������������������������������������������������435 ■ Chapter 13: Understanding TableView ���������������������������������������������������������������617 ■ Chapter 14: Understanding TreeView ����������������������������������������������������������������663 ■ Chapter 15: Understanding TreeTableView ��������������������������������������������������������689 ■ Chapter 16: Browsing Web Pages ����������������������������������������������������������������������711 v ■ Contents at a GlanCe ■ Chapter 17: Understanding 2D Shapes ��������������������������������������������������������������741 ■ Chapter 18: Understanding Text Nodes �������������������������������������������������������������789 ■ Chapter 19: Understanding 3D Shapes ��������������������������������������������������������������805 ■ Chapter 20: Applying Effects �����������������������������������������������������������������������������841 ■ Chapter 21: Understanding Transformations �����������������������������������������������������899 ■ Chapter 22: Understanding Animation ��������������������������������������������������������������917 ■ Chapter 23: Understanding Charts ��������������������������������������������������������������������953 ■ Chapter 24: Understanding the Image API ��������������������������������������������������������997 ■ Chapter 25: Drawing on a Canvas �������������������������������������������������������������������1033 ■ Chapter 26: Understanding Drag and Drop ������������������������������������������������������1043 ■ Chapter 27: Understanding Concurrency in JavaFX ����������������������������������������1071 ■ Chapter 28: Playing Audios and Videos �����������������������������������������������������������1101 ■ Chapter 29: Understanding FXML ��������������������������������������������������������������������1120 ■ Chapter 30: Understanding the Print API ��������������������������������������������������������1157 Index �������������������������������������������������������������������������������������������������������������������1173 vi Introduction Java had the support for developing GUI applications since its version 1.0 using the AWT (Abstract Windows Toolkit). Later AWT was replaced by Swing, which gave a little better user experience, but still lacked the modern-looking widgets and the support for developer’s productivity. Both AWT and Swing lacked the first-class support for data binding, efficient GUI rendering engines, easy-to-use 2D and 3D libraries for developers, and style sheet support. JavaFX was first released in 2008 as the tool to use for developing rich Internet applications (RIAs); it used a statically typed declarative language called JavaFX Script, which did not attract a lot of attention from Java developers. JavaFX 2.0, released in 2011, caught the Java community’s attention when it dropped the support for JavaFX Script and supported writing JavaFX programs using the Java programming language. In its current version, JavaFX 8 is supported in the Java platform by including the JavaFX runtime along with the Java runtime in the JRE. Now JavaFX 8 is considered a real successor for Swing for building the GUI application using the Java platform. Learn JavaFX 8 shows you how to start developing rich-client desktop applications in JavaFX 8 using your Java skills. It provides comprehensive coverage of the JavaFX 8 features. Each chapter starts with an introduction to the topic at hand. A step-by-step discussion of the topic with small snippets of code follows. At the end of the topic’s discussion, a complete program is presented. Special care has been taken to present the topics in such a way that chapters can be read serially. The book contains numerous pictures to aid you in visualizing the GUI that is built at every step in the discussion. The book starts with an introduction to JavaFX and its history. It lists the system requirements and the steps to start developing JavaFX applications. It shows you how to create a Hello World application in JavaFX, explaining every line of code in the process. Later in the book, advanced topics such as 2D and 3D graphics, charts, FXML, advanced controls, and printing are discussed. Some of the advanced controls such as TableView, TreeTableView, and WebView are covered in chapters of their own. I faced few hurdles while writing this book. As JavaFX 8 was being developed, JavaFX 2, the version before JavaFX 8, was the first release of JavaFX that used the Java programming language to write JavaFX code. There were few bugs in JavaFX 2. Sometimes it took me a couple of days of hard work to create an example to work with, only to realize that there was a bug in it. Later, if something did not work, I would look at the JIRA bug reports for JavaFX before spending too much time researching it myself. I had to fix bugs as I found them. It took me 18 months to finish this book and, in the end, it was satisfying to see that what I had produced was a lot of useful material covering almost every topic in JavaFX so fully that readers could use to learn and build a rich client application quickly using JavaFX. I hope you will enjoy the book and benefit greatly from it. I believe that programming is simple if you learn it that way. Keeping this in mind, I kept the examples in the book as simple as possible, presenting them in as few lines as I could. The examples focus on the topic being discussed. I do not present complex GUI in my examples, keeping in mind that this could obscure the learning process of the topic at hand. I have seen books that contain examples that run four or five pages long, sometimes even longer; readers of such books (myself included) often get lost in trying to understand the logic of the program, thus forgetting what they were trying to learn in that section. Therefore, simple programs in this book are intended to help you learn JavaFX faster. The book includes 330 ready-to-run programs and 430 pictures. Having more pictures than programs is evident from my approach in keeping the readers’ interest the first priority. Almost every time I discuss a snippet of code producing a UI, I include the picture of the results of the UI, so readers are not left to their imaginations as to what the code snippet will produce. Having to run every snippet of code to see the output can hinder the learning rhythm. xxvii ■ intRoduCtion Structure of the Book The book contains 30 chapters covering all topics—from basic to advanced—in JavaFX. Chapters are arranged in an order that aids you to quickly learn JavaFX. I have used an incremental approach to teach JavaFX, assuming no prior GUI development knowledge. Each chapter starts with a section introducing the topic to be discussed in the chapter. Each section contains a bit of background of the features being discussed, followed with code snippets and a complete program. What You Will Learn This book will help you to learn: • What JavaFX 8 is and its history • How to develop rich-client desktop applications using JavaFX 8 • How to use properties, collections, colors, and styles • How to use controls and handle events to build modern GUI applications • How to use advanced controls such as TreeView, TableView, and TreeTableViev. • How to access web pages in JavaFX applications • How to draw 2D and 3D shapes and apply effects and transformations • How to create animations and charts using the JavaFX 8 APIs • How to add audios and videos to your applications • How to create GUIs in JavaFX using FXML • How to provide the printing capabilities using the JavaFX Print API Who Is This Book for? Learn JavaFX 8 was written for Java developers, with beginning to intermediate level Java skills, who want to learn how to develop modern desktop GUI applications using JavaFX 8. Source code for this book may be downloaded from www.apress.com/9781484211434; errata can be submitted and viewed via the same link. Please direct all your questions and comments for the author to [email protected]. xxviii Chapter 1 Getting Started In this chapter, you will learn: • What JavaFX is • The history of JavaFX • How to write your first JavaFX application • How to use the NetBeans Integrated Development Environment to work with a JavaFX application • How to pass parameters to a JavaFX application • How to launch a JavaFX application • The life cycle of a JavaFX application • How to terminate a JavaFX Application What Is JavaFX? JavaFX is an open source Java-based framework for developing rich client applications. It is comparable to other frameworks on the market such as Adobe Flex and Microsoft Silverlight. JavaFX is also seen as the successor of Swing in the arena of graphical user interface (GUI) development technology in Java platform. The JavaFX library is available as a public Java application programming interface (API). JavaFX contains several features that make it a preferred choice for developing rich client applications: • JavaFX is written in Java, which enables you to take advantage of all Java features such as multithreading, generics, and lambda expressions. You can use any Java editor of your choice, such as NetBeans, to author, compile, run, debug, and package your JavaFX application. • JavaFX supports data binding through its libraries. • JavaFX code can be written using any Java virtual machine (JVM)-supported scripting languages such as Visage, Groovy, and Scala. • JavaFX offers two ways to build a user interface (UI): using Java code and using FXML. FXML is an XML-based scriptable markup language to define a UI declaratively. Oracle provides a tool called Scene Builder, which is a visual editor for FXML. 1 Chapter 1 ■ GettinG Started • JavaFX provides a rich set of multimedia support such as playing back audios and videos. It takes advantage of available codecs on the platform. • JavaFX lets you embed web content in the application. • JavaFX provides out-of-the-box support for applying effects and animations, which are important for developing gaming applications. You can achieve sophisticated animations by writing a few lines of code. Behind the JavaFX API lies a number of components to take advantage of the Java native libraries and the available hardware and software. JavaFX components are shown in Figure 1-1. JavaFX Public API Quantum Toolkit Prism Glass Windowing Toolkit Media Engine Web Engine Figure 1-1. Components of the JavaFX platform The GUI in JavaFX is constructed as a scene graph. A scene graph is a collection of visual elements, called nodes, arranged in a hierarchical fashion. A scene graph is built using the public JavaFX API. Nodes in a scene graph can handle user inputs and user gestures. They can have effects, transformations, and states. Types of nodes in a scene graph include simple UI controls such as buttons, text fields, two-dimensional (2D) and three-dimensional (3D) shapes, images, media (audio and video), web content, and charts. Prism is a hardware-accelerated graphics pipeline used for rendering the scene graph. If hardware- accelerated rendering is not available on the platform, Java 2D is used as the fallback rendering mechanism. For example, before using Java 2D for rending, it will try using DirectX on Windows and OpenGL on Mac Linux and embedded platforms. The Glass Windowing Toolkit provides graphics and windowing services such as windows and the timer using the native operating system. The toolkit is also responsible for managing event queues. In JavaFX, event queues are managed by a single, operating system–level thread called JavaFX Application Thread. All user input events are dispatched on the JavaFX Application Thread. JavaFX requires that a live scene graph must be modified only on the JavaFX Application Thread. Prism uses a separate thread, other than the JavaFX Application Thread, for the rendering process. It accelerates the process by rendering a frame while the next frame is being processed. When a scene graph is modified, for example, by entering some text in the text field, Prism needs to re-render the scene graph. Synchronizing the scene graph with Prism is accomplished using an event called a pulse event. A pulse event is queued on the JavaFX Application Thread when the scene graph is modified and it needs to be r e- rendered. A pulse event is an indication that the scene graph is not in sync with the rendering layer in Prism, and the latest frame at the Prism level should be rendered. Pulse events are throttled at 60 frames per second maximum. The media engine is responsible for providing media support in JavaFX, for example, playing back audios and videos. It takes advantage of the available codecs on the platform. The media engine uses a separate thread to process media frames and uses the JavaFX Application Thread to synchronize the frames with the scene graph. The media engine is based on GStreamer, which is an open source multimedia framework. The web engine is responsible for processing web content (HTML) embedded in a scene graph. Prism is responsible for rendering the web contents. The web engine is based on WebKit, which is an open source web browser engine. HTML5, Cascading Style Sheets (CSS), JavaScript, and Document Object Model (DOM) are supported. 2 Chapter 1 ■ GettinG Started Quantum toolkit is an abstraction over the low-level components of Prism, Glass, Media Engine, and Web Engine. It also facilitates coordination between low-level components. ■ Note throughout this book, it is assumed that you have intermediate-level knowledge of the Java programming language. Familiarity with the new features in Java 8 such as lambda expressions and time api is also assumed. History of JavaFX JavaFX was originally developed by Chris Oliver at SeeBeyond and it was called F3 (Form Follows Function). F3 was a Java scripting language for easily developing GUI applications. It offered declarative syntax, static typing, type inference, data binding, animation, 2D graphics, and Swing components. SeeBeyond was bought by Sun Microsystems and F3 was renamed JavaFX in 2007. Oracle acquired Sun Microsystems in 2010. Oracle then open sourced JavaFX in 2013. The first version of JavaFX was released in the fourth quarter of 2008. The current release for JavaFX is version 8.0. The version number jumped from 2.2 to 8.0. From Java 8, the version numbers of Java SE and JavaFX will be the same. The major versions for Java SE and JavaFX will be released at the same time as well. Table 1-1 contains a list of releases of JavaFX. Starting with the release of Java SE 8, JavaFX is part of the Java SE runtime library. From Java 8, you do not need any extra set up to compile and run your JavaFX programs. Table 1-1. JavaFX Releases Release Date Version Comments Q4, 2008 JavaFX 1.0 It was the initial release of JavaFX. It used a declaration language called JavaFX Script to write the JavaFX code. Q1, 2009 JavaFX 1.1 Support for JavaFX Mobile was introduced. Q2, 2009 JavaFX 1.2 Q2, 2010 JavaFX 1.3 Q3, 2010 JavaFX 1.3.1 Q4, 2011 JavaFX 2.0 Support for JavaFX script was dropped. It used the Java language to write the JavaFX code. Support for JavaFX Mobile was dropped. Q2, 2012 JavaFX 2.1 Support for Mac OS for desktop only was introduced. Q3, 2012 JavaFX 2.2 Q1, 2014 JavaFX 8.0 JavaFX version jumped from 2.2 to 8.0. JavaFX and Java SE versions will match from Java 8. 3 Chapter 1 ■ GettinG Started System Requirements You need to have the following software installed on your computer: • Java Development Kit 8 • NetBeans IDE 8.0 or later It is not necessary to have the NetBeans IDE to compile and run the programs in this book. However, the NetBeans IDE has special features for creating, running, and packaging JavaFX applications to make developers’ lives easier. You can use any other IDE, for example, Eclipse, JDeveloper, or IntelliJ IDEA. JavaFX Runtime Library All JavaFX classes are packaged in a Java Archive (JAR) file named jfxrt.jar. The JAR file is located in the jre\lib\ext directory under the Java home directory. If you compile and run JavaFX programs on the command line, you do not need to worry about setting the JavaFX runtime JAR file in the CLASSPATH. Java 8 compiler (the javac command) and launcher (the java command) automatically include the JavaFX runtime JAR file in the CLASSPATH. The NetBeans IDE automatically includes the JavaFX runtime JAR file in the CLASSPATH when you create a Java or JavaFX project. If you are using an IDE other than NetBeans, you may need to include jfxrt. jar in the IDE CLASSPATH to compile and run a JavaFX application from inside the IDE. JavaFX Source Code Experienced developers sometimes prefer to look at the source code of the JavaFX library to learn how things are implemented behind the scenes. Oracle provides the JavaFX source code. The Java 8 installation copies the source in the Java home directory. The file name is javafx-src.zip. Unzip the file to a directory and use your favorite Java editor to open the source code. Your First JavaFX Application Let’s write your first JavaFX application. It should display the text “Hello JavaFX” in a window. I will take an incremental, step-by-step approach to explain how to develop this first application. I will add as few lines of code as possible, and then, explain what the code does and why it is needed. Creating the HelloJavaFX Class A JavaFX application is a class that must inherit from the Application class that is in the javafx. application package. You will name your class HelloFXApp and it will be stored in the com.jdojo.intro package. Listing 1-1 shows the initial code for the HelloFXApp class. Note that the HelloFXApp class will not compile at this point. You will fix it in the next section. 4

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.