3 Replies Latest reply: Jan 30, 2013 3:13 AM by 987934 RSS

    Creating JavaFX Classes from Pure Java Code???

      Dear all,

      as subject stated, I want to create a javafx instance from pure java code.
      But I looked through the internet and can only find the solutions for the javafx 1.x, the one using the no longer existing fxlocal class:

      Context context = FXLocal.getContext();
      FXClassType instance = context.findClass("javatest.MyChart");
      ObjectValue obj = (ObjectValue)instance.newInstance();
      JavaInterface ji = (JavaInterface)obj.asObject();

      the question is, shouldn't there be a way to do this very easily in fx 2.x? it's now pure java, right? But apprently I can't do it, at least not so easily, after searching for a solution for many hours, I found nothing...
        • 1. Re: Creating JavaFX Classes from Pure Java Code???
          JavaFX 2.0 is all Java - there is no such thing as a JavaFX specific class.
          Any new class you create from the JavaFX API is already a Java class.

          Instead of your sample code, you could just do:
          obj = new javatest.MyChart();
          If you wanted to use the Java analog of the FXLocal constructs, then you can use the [url http://docs.oracle.com/javase/tutorial/reflect/index.htm]Java reflection API. The use of such APIs is quite rare and generally restricted to high level meta programming which requires you to dynamically instantiate a class whose type you only know at runtime.
          • 2. Re: Creating JavaFX Classes from Pure Java Code???
            It could be that the JavaFX runtime (jfxrt.jar) is not in your classpath, which would make it appear that you just can't do what you're hoping for. In my case (Linux platform) I have to add the jfxrt.jar to the classpath explicitly, but once I did that, JavaFX is simply another library that my Java code has access to.

            FWIW, moving the jfxrt.jar into the default classpath is something that is planned, if not soon, then definitely by the time Java8 his final.
            • 3. Re: Creating JavaFX Classes from Pure Java Code???
              Thanks very much, I think to include the javafx run time solved the problem...
              However, I think it's quite strange that javafx is not included in the usual java library...

              ps, I am using netbeans under windows, created a java project and want to call another javafx project with the pure java project, somehow the java project doesn't recognize the Application class of fx. But now it's solved and thanks!!!!