Methods for Java to invoke JavaFX

Source: Internet
Author: User

Java calls the JavaFX method. These three methods are :

1. Use the Scriptenginemanager class. This is the Java Scripting API (scripting API) based on the JSR-223 specification. You can run some scripts in your Java program, such as JavaFX script, JavaScript, and so on.
2. Via the JavaFX reflection API. This is the reflection API provided by JavaFX, and almost all of the JavaFX classes can be called.
3. First implement a Java interface with the JavaFX class, and then the Java program can invoke the functionality of JavaFX through this interface. The interface in the middle played a role as a bridge.


The third approach should be to compare the "normal" and "graceful" calling methods. But there is a little bit of a disadvantage: the entry of the program code must be started from JavaFX.

This is because JAVAFX programs are relatively easy to generate instances of JavaFX classes that can be used by Java parties. But sometimes we run into some specific situations and need to start the code from the Java side. For example, if you already have a more complete Java program, you need to invoke some of the functionality of JavaFX, which is a good way to use Java as a portal for your program. In order to solve this problem, I have fused the 2nd and 3rd methods, and we can take a look at the following example.

Suppose we need to invoke the JavaFX diagram (charting) feature from Java. We first generate an instance of the JavaFX class with the JavaFX reflection API. Then we'll use it in Java interface. So, let's first define a Java interface:

/* * Javainterface.java * * @author Henry Zhang      http://www.javafxblogs.com */package javatest;public interface Javain terface {public  void AddData (String name, float data);  public void Showchart ();}

The next step is to create the JavaFX class MyChart to implement this interface:

/* * MYCHART.FX * * @author Henry Zhang http://www.javafxblogs.com */package javatest;import javafx.scene.chart.PieCha Rt;import javafx.scene.scene;import javafx.scene.text.font;import Javafx.scene.text.text;import javafx.stage.Stage   Import Javafx.scene.chart.piechart3d;public class MyChart extends Javainterface {var chartdata:piechart.data[] = [];    public override function AddData (l:string, V:number): Void {var labelstring = l; var data = piechart.data {label:l value:v action:function () {println ("{labelstring} clicked!      ");    }     } ;  Insert data into ChartData; } public override function Showchart (): Void {var chart = Piechart3d {data:chartdata piethickn        Ess:25 pielabelfont:font{Size:9};        Pietolabellineonelength:10 pietolabellinetwolength:20 pielabelvisible:true Pievaluevisible:true    Translatey:-50}; Stage {title: "Piechart Window" width: 520 height:300 Scene:scene {content: [Text {font:font {s]         IZE:16} x:200 y:20 content: "Pie Chart"}, chart ]      }    }  }}

Finally, the graph function is called from the Java class Javatest :

/* * Javatest.java * @author Henry Zhang    http://www.javafxblogs.com */package javatest;import Javafx.reflect.fxclasstype;import Javafx.reflect.fxlocal;import Javafx.reflect.fxlocal.context;import Javafx.reflect.fxlocal.objectvalue;public class Javatest {public  static void Main (String args[]) {    Context context = Fxlocal.getcontext ();    Fxclasstype instance = Context.findclass ("Javatest. MyChart ");    ObjectValue obj = (ObjectValue) instance.newinstance ();    Javainterface ji = (javainterface) obj.asobject ();    String [] labels = {"January", "Febuary", "March", "April"};    int [] values = {, +, +, PNS};    for (int i=0; i < values.length; i++) {      ji.adddata (labels[i], values[i]);    }    Ji.showchart ();  }}

In the code, these 3 sentences are created with the JavaFX class javatest. MyChart instances,

    Context context = Fxlocal.getcontext ();    Fxclasstype instance = Context.findclass ("Javatest. MyChart ");    ObjectValue obj = (ObjectValue) instance.newinstance ();

And this is the way to convert JavaFX instances into Java-usable objects:

    Javainterface ji = (javainterface) obj.asobject ();

If you are using the NetBeans IDE, you can set the javatest.JavaTest class as the main class (The Startup Class) in the project properties. A javatest.jar file is generated after compilation. Results of program operation

You can do this in the command line in the following ways:

   Javafx-jar Javatest.jar

In fact, you can start the program in a pure Java way, as long as you bring the JAVAFX environment into operation, such as:

Java-djava.library.path= "<path to JavaFX SDK lib>"     -classpath "<all JavaFX SDK jars>"-jar Javatest.jar

Because JavaFX requires a lot of jar files, this "purest" Java method is cumbersome to use. I think it's simpler to use the javafx command, and JavaFX is the encapsulation of the above Java commands.

(The above content is excerpted from: http://www.th7.cn/Program/java/201510/616465.shtml)

Methods for Java to invoke JavaFX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.