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

Using javafxc to compile JavaFX code


While it certainly makes it easier to build JavaFX with the support of an IDE (see the NetBeans and Eclipse recipes), it is not a requirement. In some situations, having direct access to the SDK tools is preferred (automated build for instance). This recipe explores the build tools that are shipped with the JavaFX SDK and provides steps to show you how to manually compile your applications.

Getting ready

To use the SDK tools, you will need to download and install the JavaFX SDK. See the recipe Installing the JavaFX SDK for instructions on how to do it.

How to do it...

Open your favorite text/code editor and type the following code. The full code is available from ch01/source-code/src/hello/HelloJavaFX.fx.

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

Save the file at location hello/Main.fx.

To compile the file, invoke the JavaFX compiler from the command line from a directory up from the where the file is stored (for this example, it would be executed from the src directory):

javafxc hello/Main.fx

If your compilation command works properly, you will not get any messages back from the compiler. You will, however, see the file HelloJavaFX.class created by the compiler in the hello directory.

If, however, you get a "file not found" error during compilation, ensure that you have properly specified the path to the HelloJavaFX.fx file.

How it works...

The javafxc compiler works in similar ways as your regular Java compiler. It parses and compiles the JavaFX script into Java byte code with the .class extension.

javafxc accepts numerous command-line arguments to control how and what sources get compiled, as shown in the following command:

javafxc [options] [sourcefiles] [@argfiles]

where options are your command-line options, followed by one or more source files, which can be followed by list of argument files. Below are some of the more commonly javafxc arguments:

  • classpath (-cp) - the classpath option specifies the locations (separated by a path separator character) where the compiler can find class files and/or library jar files that are required for building the application.

    javafxc -cp .:lib/mylibrary.jar MyClass.fx
    
  • sourcepath - in more complicated project structure, you can use this option to specify one or more locations where the compiler should search for source file and satisfy source dependencies.

    javafxc -cp . -sourcepath .:src:src1:src2 MyClass.fx
    
  • -d - with this option, you can set the target directory where compiled class files are to be stored. The compiler will create the package structure of the class under this directory and place the compiled JavaFX classes accordingly.

    javafxc -cp . -d build MyClass.fx
    

    When specifying the source files, you can use the wild card characters to indicate multiple source files to be compiled as follows:

    javafxc -d build src/*.fx
    
  • The @argfiles option lets you specify a file which can contain javafxc command-line arguments. When the compiler is invoked and a @argfile is found, it uses the content of the file as an argument for javafxc. This can help shorten tediously long arguments into short, succinct commands.

    Assume file cmdargs has the following content

    -d build
    -cp .:lib/api1.jar:lib/api2.jar:lib/api3.jar
    -sourcepath core/src:components/src:tools/src
    

    Then you can invoke javafxc as:

    $> javafxc @cmdargs
    

See also

  • Installing the JavaFX SDK