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

Integrating your JavaFX code with Java


JavaFX is built directly on top of the Java Virtual Machine (JVM). Therefore, your JavaFX code has access to the entire Java ecosystem including all of the standard Java libraries such as IO, JDBC, XML, Swing, and so on. Any compiled Java code accessible on the class path can be called from within a JavaFX script. This recipe covers the techniques required to integrate JavaFX and Java code together.

Getting ready

This section explore integration techniques between JavaFX and Java. You should have familiarity with the Java language, its libraries, or have the ability to create your own classes or libraries to be called from JavaFX.

How to do it...

The easiest way to see Java and JavaFX interoperate is to create an instance of a Java object and invoke a method on the instance from within JavaFX. Let's go through an example. You can see the full code listing in package ch01/source-code/src/java.

First create and compile this simple class:

public class JavaObject {
private String name;
public JavaObject(String n){
name = n;
}
public void printReverse() {
for(int i = name.length()-1; i >= 0; i--){
System.out.print (name.charAt(i));
}
System.out.println();
}
}

Now create a JavaFX script which creates an instance of JavaObject and invoke the the printReverse() method on the class.

var javaObject = new JavaObject("Hello World!");
javaObject.printReverse();

How it works...

Java classes and JavaFX classes are binary-compatible. When you compile your JavaFX classes, the JavaFX compiler creates a Java class file (a.class extension file). There are three points that should be made regarding the code snippet in this recipe:

  1. 1. Similar to Java, JavaFX script supports the new operator when creating a new object instance. This makes it easy to instantiate objects written in Java from within JavaFX.

Note

While JavaFX objects can be instantiated using Object Literal Notation and the new operator, Java objects can only be instantiated with the new operator.

  1. 2. The type inference engine will automatically determine the type of the assignment using the Java object's type.

  2. 3. Once you have access to the Java object instance, you may invoke any public members on that object.

There is more...

In JavaFX, not only can you instantiate pure Java classes, you can also implement Java interfaces directly. Using this mechanism, you can achieve two-way integration between Java and JavaFX. Again, the full listing of the code presented here can be found in package ch01/source-code/src/java.

Implementing a Java interface in JavaFX

The steps required to implement a Java interface in JavaFX are simple. You first create a JavaFX class which extends the interface. Then, you provide JavaFX functions which implement methods defined in the interface, as given the following Java interface:

interface JavaInterface {
int add(int num1, int num2);
}

You can create JavaFX script with the following implementation:

public class JavaInterfaceImpl extends JavaInterface {
override function add(num1, num2) {
num1 + num2;
}
}
public function run() {
var adder = JavaInterfaceImpl { }
println(adder.add(1, 2));
}

Note that in JavaFX, the extends keyword is used to implement the interface instead of implements as in Java.

Note that there are other ways to achieve integration between Java and JavaFX. The rules vary depending on the level of integration you are seeking:

  1. 1. Type integration using Java types from JavaFX, as shown in this recipe.

  2. 2. Framework integration for example, calling Swing components from JavaFX.

  3. 3. API integration wrapping native Java libraries within JavaFX classes to expose them as JavaFX components

See also

  • Creating and using JavaFX classes

  • Declaring and using variables in JavaFX