Book Image

JavaFX 1.2 Application Development Cookbook

By : Vladimir Vivien
Book Image

JavaFX 1.2 Application Development Cookbook

By: Vladimir Vivien

Overview of this book

JavaFX Script enables you to easily create rich Internet applications by embedding multimedia components. Although you can create stylish Internet applications by modifying these default components, even advanced users find it challenging to create impressive feature-rich Internet applications with JavaFX. Also, there are limited JavaFX components to work with and by default these components don't look visually appealing.This book explores limitless possibilities to style your application by coding JavaFX components to display your content in a more appealing fashion. The recipes in this book will help you to create customized JavaFX components with which you can make modern, feature-rich applications.First, you will be introduced to the JavaFX SDK and other development tools available to help you be productive during development. You will create an application in JavaFX by arranging complex graphical components (and non-graphical libraries) with simplified declarative constructs. You will then explore the fun side of JavaFX by using transformation techniques to manipulate the location and dimensions of objects. The next chapter is about the GUI components that are available in the framework, which provide a high level of interactivity. You will learn how to use the media component to play media content. Then we will access data and manipulate data locally or remotely. You will explore many deployment options and integration tips and tricks to take advantage of runtime contexts. Finally, you will interact with pure Java code to read and write files in JavaFX and to establish interactions with computing platforms.
Table of Contents (18 chapters)
JavaFX 1.2 Application Development Cookbook
Credits
About the Author
About the Reviewers
Preface
Mobile JavaFX
JavaFX Composer
JavaFX Products and Frameworks
Best Practices for Development
Best Practices for Deployment

Setting up JavaFX for the Eclipse IDE


As of JavaFX version 1.2, Sun Microsystems the name (will be Oracle by the time you read this) officially released a fully functional plugin to support development in the Eclipse IDE. While the Eclipse plugin came after NetBean's, it still packs an invaluable set of functionalities for developers who feel more comfortable working in Eclipse, including:

  • Project creation wizard and templates

  • Syntax highlighting

  • Code completion

  • Error detection

  • Code block formatting and folding

  • In-editor API documentation

  • Debugging

  • Continuous background build

  • And more…

This recipe shows how to set up the Eclipse IDE for JavaFX development. You will learn how to configure Eclipse and the JavaFX 1.2 plugin.

Getting ready

Before you can start building JavaFX applications in the Eclipse IDE, you must ensure that your development environment meets the minimum requirements for JavaFX 1.2, which requires Eclipse 3.4 (Ganamede) for Java EE developers (or higher). To get the Eclipse plugin to work properly, ensure that you have downloaded and configured the Java JDK and the JavaFX SDK (see the recipe Installing the JavaFX SDK for details).

How to do it...

As with anything else in Eclipse, JavaFX support comes in the form of a plugin. You have to download and configure the plugin to work with your previously installed local JavaFX SDK prior to building your applications. To get started, do the following:

  1. 1. Select Software Updates from the Help menu to open the Plugins management window.

  2. 2. Click on the Available Software tab.

  3. 3. Add the site http://javafx.com/downloads/eclipse-plugin/ as the plugin site.

  1. 4. Select the JavaFX site, as shown in the previous screenshot, then click on Install to continue.

  2. 5. Follow the instructions of the plugin wizard.

  3. 6. Accept the terms of the license, and make sure to restart the Eclipse IDE when prompted.

Now that you have Eclipse setup with JavaFX, it makes sense to create a quick Hello World application in Eclipse to test the installation.

To get started, select New from the File menu (you may have to select Other if JavaFX is not listed as a project type).

Note

You must have the SDK installed and configured prior to creating your first application (see recipe Installing the JavaFX SDK).

  1. 7. When presented with the new project wizard, select JavaFX Project and click on the Next button.

  2. 8. Then, provide the project's name (HelloWorld), location, JRE version, and type (default is Desktop) to continue with the project's creation.

  3. 9. Click on the Next button and select a project template (which is a based on pre-existing sample code). Select the Empty Project template and click on the Finish button.

  4. 10. The wizard will complete the project creation, and you should have a project shell ready for you to start coding.

  5. 11. To continue, create a new code package (right-click on the project source directory, and select New | Package), and name the package hello.

  6. 12. Next, right-click on the newly created source package and select New | Empty JavaFX Script from the context menu, and name it HelloJavaFX.fx.

  7. 13. This will do exactly what it says, which is to create an empty code window. Notice, however, that the editor comes with several code snippets that you can reuse in your own code.

  8. 14. In the Snippets window, shown in the previous screenshot, click on Applications and double-click on Stage. This will bring up a template editor. Accept the default values and continue.

Edit the sample code by adding the highlighted portion. You can see the full code listing at ch01/source-code/src/hello/HelloJavaFX.fx.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
Stage {
title: "Hello JavaFX"
scene: Scene {
width: 250
height: 200
content: [
Text {
font : Font {size : 16}
x: 10
y: 30
content: "Hello World!"
}
]
}

Once you have updated the code, right-click on the project and select Run As | JavaFX Application. If you are running the application for the first time, you will be prompted to select the application's targeted profile and the main class.

How it works...

Support for JavaFX in Eclipse comes as separate plugin download. When you install the plugin, it adds the capabilities of JavaFX development to your IDE. Unlike the NetBeans plugin, as of version 1.2, the SDK is not available as part of the plugin download. You must download and have the SDK installed on your workstation. When the plugin is installed, it will look for the SDK on your machine.

See also

  • Installing the JavaFX SDK

  • Setting up JavaFX for the NetBeans IDE