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 JavaFX classes


JavaFX is an object-oriented scripting language. As such, object types, represented as classes, are part of the basic constructs of the language. This section shows how to declare, initialize, and use JavaFX classes.

Getting ready

If you have used other scripting languages such as ActionScript, JavaScript, Python, or PHP, the concepts presented in this section should be familiar. If you have no idea what a class is or what it should be, just remember this: a class is code that represents a logical entity (tree, person, organization, and so on) that you can manipulate programmatically or while using your application. A class usually exposes properties and operations to access the state or behavior of the class.

How to do it...

Let's assume we are building an application for a dealership. You may have a class called Vehicle to represent cars and other type of vehicles processed in the application. The next code example creates the Vehicle class. Refer to ch01/source-code/src/javafx/Vehicle.fx for full listing of the code presented here.

  1. 1. Open your favorite text editor (or fire up your favorite IDE).

  2. 2. Type the following class declaration.

    class Vehicle {
    var make;
    var model;
    var color;
    var year;
    function drive () : Void {
    println("You are driving a "
    "{year} {color} {make} {model}!")
    }
    }
    
  3. 3. Once your class is properly declared, it is now ready to be used. To use the class, add the following (highlighted code) to the file:

    class Vehicle {
    ...
    }
    var vehicle = Vehicle {
    year:2010
    color: "Grey"
    make:"Mini"
    model:"Cooper"
    };
    vehicle.drive();
    
  4. 4. Save the file as Vehicle.fx. Now, from the command-line, compile it with

    $> javafxc Vehicle.fx
    

    Note

    If you are using an IDE, you can simply right, click on the file to run it.

  • When the code executes, you should see:

    $> You are driving a 2010 Grey Mini Cooper!
    

How it works...

The previous snippet shows how to declare a class in JavaFX. Albeit a simple class, it shows the basic structure of a JavaFX class. It has properties represented by variables declarations:

var make;
var model;
var color;
var year;

and it has a function:

function drive () : Void {
println("You are driving a "
"{year} {color} {make} {model}!")
}

which can update the properties and/or modify the behavior (for details on JavaFX functions, see the recipe Creating and Using JavaFX functions). In this example, when the function is invoked on a vehicle object, it causes the object to display information about the vehicle on the console prompt.

Object literal initialization

Another aspect of JavaFX class usage is object declaration. JavaFX supports object literal declaration to initialize a new instance of the class. This format lets developers declaratively create a new instance of a class using the class's literal representation and pass in property literal values directly into the initialization block to the object's named public properties.

var vehicle = Vehicle {
year:2010
color: "Grey"
make:"Mini"
model:"Cooper"
};

The previous snippet declares variable vehicle and assigns to it a new instance of the Vehicle class with year = 2010, color = Grey, make = Mini, and model = Cooper. The values that are passed in the literal block overwrite the default values of the named public properties.

There's more...

JavaFX class definition mechanism does not support a constructor as in languages such as Java and C#. However, to allow developers to hook into the life cycle of the object's instance creation phase, JavaFX exposes a specialized code block called init{} to let developers provide custom code which is executed during object initialization.

Initialization block

Code in the init block is executed as one of the final steps of object creation after properties declared in the object literal are initialized. Developers can use this facility to initialize values and initialize resources that the new object will need. To illustrate how this works, the previous code snippet has been modified with an init block. You can get the full listing of the code at ch01/source-code/src/javafx/Vehicle2.fx.

class Vehicle {
...
init {
color = "Black";
}
function drive () : Void {
println("You are driving a "
"{year} {color} {make} {model}!");
}
}
var vehicle = Vehicle {
year:2010
make:"Mini"
model:"Cooper"
};
vehicle.drive();

Notice that the object literal declaration of object vehicle no longer includes the color declaration. Nevertheless, the value of property color will be initialized to Black in the init{} code block during the object's initialization.

When you run the application, it should display:

You are driving a 2010 Black Mini Cooper!

See also

  • Declaring and using variables in JavaFX

  • Creating and using JavaFX functions