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

Creating and using variables in JavaFX


JavaFX is a statically type-safe and type-strict scripting language. Therefore, variables (and anything which can be assigned to a variable, including functions and expressions) in JavaFX, must be associated with a type, which indicates the expected behavior and representation of the variable. This sections explores how to create, initialize, and update JavaFX variables.

Getting ready

Before we look at creating and using variables, it is beneficial to have an understanding of what is meant by data type and be familiar with some common data types such as String, Integer, Float, and Boolean. If you have written code in other scripting languages such as ActionScript, Python, and Ruby, you will find the concepts in this recipe easy to understand.

How to do it...

JavaFX provides two ways of declaring variables including the def and the var keywords.

def X_STEP = 50;
prntln (X_STEP);
X_STEP++; // causes error
var x : Number;
x = 100;
...
x = x + X_LOC;

How it works…

In JavaFX, there are two ways of declaring a variable:

  • def The def keyword is used to declare and assign constant values. Once a variable is declared with the def keyword and assigned a value, it is not allowed be reassigned a new value.

  • var The var keyword declares variables which are able to be updated at any point after their declaration.

There's more...

All variables must have an associated type. The type can be declared explicitly or be automatically coerced by the compiler. Unlike Java (similar to ActionScript and Scala), the type of the variable follows the variable's name separated by a colon.

var location:String;

Explicit type declaration

The following code specifies the type (class) that the variable will receive at runtime:

var location:String;
location = "New York";

The compiler also supports a short-hand notation that combines declaration and initialization.

var location:String = "New York";

Implicit coercion

In this format, the type is left out of the declaration. The compiler automatically converts the variable to the proper type based on the assignment.

var location;
location = "New York";

Variable location will automatically receive a type of String during compilation because the first assignment is a string literal.

Or, the short-hand version:

var location = "New York";

JavaFX types

Similar to other languages, JavaFX supports a complete set of primitive types as listed:

:String this type represents a collection of characters contained within within quotes (double or single, see following). Unlike Java, the default value for String is empty ("").

"The quick brown fox jumps over the lazy dog" or
'The quick brown fox jumps over the lazy dog'

:Number this is a numeric type that represents all numbers with decimal points. It is backed by the 64-bit double precision floating point Java type. The default value of Number is 0.0.

0.01234
100.0
1.24e12

:Integer this is a numeric type that represents all integral numbers. It is backed by the 32-bit integer Java type. The default value of an Integer is 0.

-44
7
0
0xFF

:Boolean as the name implies, this type represents the binary value of either true or false.

:Duration this type represent a unit of time. You will encounter its use heavily in animation and other instances where temporal values are needed. The supported units include ms, s, m, and h for millisecond, second, minute, and hour respectively.

12ms
4s
12h
0.5m

:Void this type indicates that an expression or a function returns no value. Literal representation of Void is null.

Variable scope

Variables can have three distinct scopes, which implicitly indicates the access level of the variable when it is being used.

Script level

Script variables are defined at any point within the JavaFX script file outside of any code block (including class definition). When a script-level variable is declared, by default it is globally visible within the script and is not accessible from outside the script (without additional access modifiers).

Instance level

A variable that is defined at the top-level of a class is referred to as an instance variable. An instance level is visible within the class by the class members and can be accessed by creating an instance of the class.

Local level

The least visible scope are local variables. They are declared within code blocks such as functions. They are visible only to members within the block.

See also

  • Creating and using JavaFX classes

  • Creating and using JavaFX functions