views:

123

answers:

2

I want to access a static JavaFX class member from Java using the Javafx reflection API.

E.g. JavaFX code:

public var thing;

class MyJavaFXClass {
}

Java code:

private Object getThing() {
  FXClassType classType = FXContext.getInstance().findClass("mypackage.MyJavaFXClass");

  // Get static member 'thing' from 'MyJavaFXClass'
  // <Insert Code Here>

  return thing;
}

What Java code do I need to access 'MyJavaFXClass.thing'?

Note: I am using JavaFX 1.3 - I'm not sure if the reflection API is different here to earlier JavaFX versions.

A: 

Your "MyJavaFXClass" should implement an interface IF. The interface IF should define at least one method that returns the type of your "var thing". Your "MyJavaFXClass" then must implement the interface using "class MyJavaFXClass extends IF".

To access your "thing" from Java code, first cast the Object to IF and then call the method.

Cake3d
+1  A: 
FXClassType classType = FXContext.getInstance().findClass("mypackage.MyJavaFXClass");
FXVarMember var =  classType.getVariable("thing");
FXValue value = var.getValue(null);
System.out.println(value.getValueString());

or if you want the Object, not the String.

FXLocal.Value value = (FXLocal.Value)var.getValue(null);
Object obj = value.asObject();
System.out.println(obj);
JimClarke
Thanks Jim, that's perfect. offtopic: by the way, I want to thank you for figuring out how to embed JavaFX 1.3 scenes into a Swing app - saved me an inordinate amount of headache!!
James