Sei sulla pagina 1di 6

Writing Java code for JavaFX Script | Mike's Blog

Home

Mike's Blog
JavaFX Script

Articles related to JavaFX


JavaFX Mobile FXUtil Uncategorized JavaFX

Writing Java code for JavaFX Script

Mar

15

About me

Suchen...

When writing Java classes which can be used in JavaFX scripts, three factors have to be considered: how to create a Java object, how to access its members and how to pass parameters between Java and JavaFX.

Recent Posts
Upcoming property changes Creating JavaFX Properties JavaFX Properties Michael Heinrichs Age: 36 years Location: Prague Technical Lead JavaFX Properties Binding API Animation API JavaFX Collections Hobby Projects: FXUtil Game Center Migration to new domain finished New version of library for inverse kinematics released

Creating Java Objects


Creating Java objects from a JavaFX script is fairly simple, as this functionality is already built into the language. To setup a class, which can be instantiated in a JavaFX script, you just have to provide constructors as you would for the usage in Java classes. In Code Sample 1, a class with two constructors is created.
public class MyJavaClass { private String property; public MyJavaClass() { this.property = "Hello World"; }; public MyJavaClass(String property) { this.property = property; }; public void printProperty() { System.out.println(property); } }

Popular Posts
Writing Java code for JavaFX Script Using JavaFX objects in Java code Binding Java Objects in JavaFX Script Best Practices for JavaFX Mobile Applications Tutorial for Inverse Kinematics Part 1 is published

Latest Tweets
JavaFX Daily is out! http://t.co/REpm84MJ Top stories today via @htmlzen @etorreborre @alltop_java @adriankyu 11 hours ago RT @DrTwittenheimer : After the break-up, Pong went on to become an extremely influential video game. Ping, sadly, just tests internet co ... 20 hours ago JavaFX Daily is out! http://t.co/REpm84MJ Top stories today via @fxexperience @devstonez @jaxentercom 1 day ago

Tags

Code Sample 1: Definition of MyJavaClass To create an instance of MyJavaClass in a JavaFX script, simply call its constructor. Notify that the brackets can be omitted, if a constructor without parameters is called.
var o1 = new MyJavaClass; var o2 = new MyJavaClass("Goodbye World!");

best_practices
inverse_kinematics

javafx

mobile

Recent Comments
Mike on JavaFX Properties Mike on Upcoming property changes Cowra on Upcoming property changes LH Seadorf on JavaFX Properties Mike on JavaFX Properties

Code Sample 2: Instantiating MyJavaClass in JavaFX Script

Accessing Members
Accessing members of Java objects from a JavaFX script works as it would in a pure Java environment. The access modifiers public, protected and private imply the same restrictions. As you will see in the example about passing parameters, you can also access static members defined in your Java classes. The following lines create an instance of MyJavaClass and call its method printProperty.
var o1 = new MyJavaClass("Goodbye World!"); o1.printProperty();

Code Sample 3: Accessing a member of MyJavaClass in JavaFX Script

http://blog.netopyr.com/2008/03/15/writing-java-code-for-javafx-script/[9/21/2011 10:14:35 AM]

Writing Java code for JavaFX Script | Mike's Blog

Passing Parameters
When it comes to passing parameters, there are four categories of data types to consider. They are listed in the following table. JavaFX Type Java Type Number Integer Boolean String Java objects JavaFX objects Sequences Passing primitive data types and Java objects is straightforward. The primitive data types Number, Integer, Boolean, and String are mapped to primitive data types or the equivalent objects as shown in the table. In Code Sample 4, a Java class is defined with methods to take these data types and return them.
public class MyLibrary { public static double returnDoublePrimitive (double d) { return d; } public static Double returnDoubleObject (Double d) { return d; } public static int returnIntegerPrimitive (int i) { return i; } public static Integer returnIntegerObject (Integer i) { return i; } public static boolean returnBooleanPrimitive (boolean b) { return b; } public static Boolean returnBooleanObject (Boolean b) { return b; } public static String returnString (String s) { return s; } }

double / java.lang.Double int / java.lang.Integer boolean / java.lang.Boolean java.lang.String

Code Sample 4: Definition of MyLibrary The JavaFX script in Code Sample 5 calls the Java-methods. As the code sample shows, it makes no difference to the JavaFX script, if a primitive data type or the corresponding object is needed as a parameter.
var n1: Number = 3.1415; var n2: Number; n2 = MyLibrary.returnDoublePrimitive(n1); n2 = MyLibrary.returnDoubleObject(n1); var i1: Integer = 42; var i2: Integer; i2 = MyLibrary.returnIntegerPrimitive(i1); i2 = MyLibrary.returnIntegerObject(i1); var b1: Boolean = true; var b2: Boolean; b2 = MyLibrary.returnBooleanPrimitive(b1); b2 = MyLibrary.returnBooleanObject(b1); var s1: String = "Passing a String"; var s2: String;

http://blog.netopyr.com/2008/03/15/writing-java-code-for-javafx-script/[9/21/2011 10:14:35 AM]

Writing Java code for JavaFX Script | Mike's Blog s2 = MyLibrary.returnString(s1);

Code Sample 5: Passing primitive JavaFX data types When defining a new Java interface, Id recommended to use only the data types, which are defined in JavaFX. If that is not possible, the data has to be cast. In most cases, this is done implicitly, but in some cases it has to be done explicitly using the methods byteValue(), shortValue(), longValue(), and floatValue(). To pass a char, the method charAt() of the data type String can be used. In Code Sample 6, a Java class is defined which expects these data types. There also exist two methods doubleValue() and intValue(), which can be used to cast a Number to an Integer and vice versa. These methods can also be used with literals directly, e.g. 42.floatValue() casts the magic number to a float.
public class MyLibraryCast { public static byte returnBytePrimitive (byte b) { return b; } public static Byte returnByteObject (Byte b) { return b; } public static short returnShortPrimitive (short s) { return s; } public static Short returnShortObject (Short s) { return s; } public static long returnLongPrimitive (long l) { return l; } public static Long returnLongObject (Long l) { return l; } public static float returnFloatPrimitive (float f) { return f; } public static Float returnFloatObject (Float f) { return f; } public static char returnCharacterPrimitive (char c) { return c; } public static Character returnCharacterObject (Character c) { return c; } }

Code Sample 6: Definition of MyLibraryCast Code sample 7 shows how these methods can be called. In these cases there is a difference between using the primitive data types in Java and using the equivalent objects as you can see in the method-calls below.
var i1: Integer = 42; var i2: Integer; i2 = MyLibraryCast.returnBytePrimitive(i1); i2 = MyLibraryCast.returnByteObject(i1.byteValue()); i2 = MyLibraryCast.returnShortPrimitive(i1); i2 = MyLibraryCast.returnShortObject(i1.shortValue()); i2 = MyLibraryCast.returnLongPrimitive(i1) as Integer; i2 = MyLibraryCast.returnLongObject(i1.longValue()).intValue(); var n1: Number = 3.1415;

http://blog.netopyr.com/2008/03/15/writing-java-code-for-javafx-script/[9/21/2011 10:14:35 AM]

Writing Java code for JavaFX Script | Mike's Blog var n2: Number; n2 = MyLibraryCast.returnFloatPrimitive(n1.floatValue()); n2 = MyLibraryCast.returnFloatObject(n1.floatValue()); var s1: String = "c"; var s2: String; s2 = java.lang.Character.toString (MyLibraryCast.returnCharacterPrimitive (s1.charAt(0)) ); s2 = MyLibraryCast.returnCharacterObject(s1.charAt(0)).toString();

Code Sample 7: Passing primitive Java data types by casting A Java object does not need to be mapped, but is passed as the very same object. In the Java code it makes no difference if the Java object was passed from a JavaFX script or from some other Java code. The static method in Code Sample 8 take an instance of MyJavaClass (see Code Sample 1), calls its method printProperty and returns the object.
public class MyLibraryForObjects { public static MyJavaClass printAndReturnMyJavaClass (MyJavaClass o) { o.printProperty(); return o; } }

Code Sample 8: Definition of MyLibraryForObjects The JavaFX script in Code Sample 9 creates an instance of MyJavaClass, passes it to the method printAndReturnMyJavaClass and saves the result.
var o1: MyJavaClass = new MyJavaClass("Hello Java!"); var o2: MyJavaClass; o2 = MyLibraryForObjects.printAndReturnMyJavaClass(o1);

Code Sample 9: Passing a Java object Passing JavaFX objects and sequences to Java is slightly more difficult and will be covered in the next articles.

Tags: best_practices, javafx Erstellt von Mike JavaFX Script Abonniere RSS feed

10 Comments on Writing Java code for JavaFX Script


1.

Alex says:
April 10, 2008 at 5:14 am Hi Mike, Can you please tell us, what casting is done in your MyLibraryCast class, as each method in it gets and returns a value of same type. AFAIK casting means converting from one type to another, like String to Integer. Is it so ?

2.

Michael Heinrichs says:


April 10, 2008 at 5:48 am The name of the library is really badly chosen. Actually I wanted to express, that some casting needs to be done to call the methods of this library. Byte, Short etc. are not primitive datatypes in JavaFX Script. The casting is done in the JavaFX

http://blog.netopyr.com/2008/03/15/writing-java-code-for-javafx-script/[9/21/2011 10:14:35 AM]

Writing Java code for JavaFX Script | Mike's Blog

code sample below (e.g. i1.byteValue() to cast an Integer (indirectly) to a Byte). 3.

fabio says:
May 16, 2008 at 4:22 pm amigo. porque quando eu fao uma classe em java e depois vou instaciar em outro arquivo javafx fica dando erro de compilao dizendo que no esta definida o tipo.

4.

fabio says:
May 16, 2008 at 4:22 pm amigo. porque quando eu fao uma classe em java e depois vou instanciar em outro arquivo javafx fica dando erro de compilao dizendo que no esta definida o tipo.

5.

Luigi says:
June 20, 2008 at 6:36 am hi, i haved insalled the eclipse pluging and try this but i cant call the java-class why?

6.

Marko says: October 14, 2008 at 11:30 am


Here you deal only with primitive types. What I have is array of objects that I would like to cast into Sequence in order to use it JavaFX. Any help?

7.

Moprano says: April 21, 2009 at 8:28 am


Any feedback to Markos request? I am in the same boat.

8.

LaoZhang says:
June 22, 2009 at 7:56 pm Moprano & Marko, i think the better way is just use the Array instead of any collections in your java classes. Then you can simply refer them in your FX script.

9. Hi!

rtkrvj says:
September 2, 2009 at 8:20 am

On following code samples 5 and 6, I have created class MyLibrary in the same package and declared methods as : public static double returnDoublePrimitive (double d) { return d; } public static Double returnDoubleObject (Double d) { return d; }i am getting and this in the fx class which is also in the same package, var n1: Number = 3.1415; var n2: Number; n2 = MyLibrary.returnDoublePrimitive(n1); n2 = MyLibrary.returnDoubleObject(n1); But the fx file gives this error. There is a garbled class member definition starting here, I have tried to ignore it. Class members can be var, def, function, init or postinit only. please suggest.

http://blog.netopyr.com/2008/03/15/writing-java-code-for-javafx-script/[9/21/2011 10:14:35 AM]

Writing Java code for JavaFX Script | Mike's Blog

10.

davix says:
October 30, 2009 at 10:04 pm I need the other way around, I mean, seting a javafx instance in a java class thx

RSS
Copyright 2011 Mike's Blog All rights reserved. Blue Grace theme by Vladimir Prelovac

http://blog.netopyr.com/2008/03/15/writing-java-code-for-javafx-script/[9/21/2011 10:14:35 AM]

Potrebbero piacerti anche