Talking about the interoperability between Java and JavaFX

Source: Internet
Author: User
Tags reflection

Read an article from JavaFX's official blog about how to invoke the JavaFX class from Java code. The situation now is that JavaFX can invoke Java classes without any restrictions, whereas Java, in turn, can not invoke JavaFX classes casually. This can be seen in the compilation process of the JavaFX project. In NetBeans, for example, the build process is to compile the Java Code (JAVAC) First, then the JavaFX code (JAVAFXC), so that the Java code does not know there is a JavaFX class, and the JavaFX class can "see" the Java class. Search for a bit we can see that many programmers are looking for a variety of ways to invoke the JavaFX class from Java. There is an interesting article about how to analyze the structure of the JavaFX class by reverse engineering. Even that article on the JavaFX Web site uses a non-standard API to do this, and also "guarantees" that this method will be invalidated in the next release.

So do we really need this interoperability between Java and JavaFX? I think this interoperability is necessary. If the two can be approximate to the degree of mixing, in the long run, JavaFX can have greater vitality. Imagine using MVC design pattern (model-view-controller), we can use Java and JavaFX together to develop the application: in Java to write "M" and "C" two parts, with JavaFX to write "V" part, this will be very interesting thing.

Currently, there are several "standard" ways to invoke JavaFX from Java.

1. Using the Scirptenginemanager class, the article mentions that we can do this:

package calc;
import java.io.InputStreamReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class CalculatorLauncher {
public static void main(String[] args) {
try {
ScriptEngineManager manager=new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("fx");
InputStreamReader reader = new InputStreamReader (CalculatorLauncher.cla
ss.getResourceAsStream("Calculator.fx"));
engine.eval(reader); } catch (ScriptException ex) { }
}
}

However, this approach does not make much sense because it does a system call like System.exec ("Calc"). I think it's better to use System.exec ("JavaFX calculator.fx") more directly.

2. Use Java reflection to parse the bytecode of JavaFX, get each method or attribute, then make various calls. In principle, this is feasible. But because the reflection is very complex, the usability is greatly reduced, at the same time, the code also has no readability.

3. The third approach is to define a Java interface and then implement this interface in JavaFX. For example:

public interface JavaInterface { ... }

In myjavafxclass.fx, you can write this:

public class MyJavaFXClass extends JavaInterface
{ ... }

In Java code, simply follow the interface to invoke the JavaFX object. This approach can solve most interoperability problems. The only trouble is having to define a whole bunch of interface, but this is one of the best solutions I've found in my current position.

JavaFX is now the first release, so we don't have to ask too much of it. But I still hope that JavaFX's designers will consider this issue carefully in the next release.

Related Article

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.