views:

789

answers:

3

Can I utilise the new functionality provided by the new JavaFX APIs directly from Java to the same extent as I would be able to using JavaFX Script?

Are all the underlying JavaFX APIs purely Java or JavaFX Script or a mix?

+3  A: 

The JavaFX APIs are a mix of JavaFX and Java. The SDK comes with an archive src.zip which contains a part of the APIs (only the most basic classes are included, but things like javafx.scene are missing).

Calling JavaFX code from Java is not officially supported in JavaFX 1.x AFAIK. There's a blog entry in the JavaFX blog that shows you how to do anyway it using unsupported APIs, but it's complicated and won't work this way in future versions.

There are two supported ways to use JavaFX from Java. Either you use the Scripting API to invoke JavaFX code, as shown in this article. Or, which is the most elegant solution IMHO, write the API-accessing code using JavaFX, define Java interfaces to interact with your JavaFX code from plain Java, and then implement those interfaces in JavaFX.

Tim Jansen
+1  A: 

We were able to use the multi-media components with JavaFX with some success. they are totally unsupported in this manner and subject to change similar to how the com.sun packages are in JavaSE.

We integrated them into a Swing UI and were able to do the MM stuff you could do from FX straight from Java.

Hope they put some of that in the core libraries soon.

Matt
+1  A: 

The scenegraph used in JavaFX is opensource. You can check it here (https://scenegraph.dev.java.net). While the site hasn't been updated to reflect the final version shipped with JavaFX 1.x, you can still use the jar that comes with the JavaFX SDK and mix scenegraph and swing nodes inside your swing application. However, you'll have some difficulty because there's no official API for this version of scenegraph.

Here's a "hello world" using the scenegraph that comes woth JavaFX 1.0. Remember to include the "Scenario.jar" in your build path.

import java.awt.Color;
import java.awt.Paint;
import java.awt.geom.Point2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.sun.scenario.scenegraph.JSGPanel;
import com.sun.scenario.scenegraph.SGGroup;
import com.sun.scenario.scenegraph.fx.FXText;



public class HelloWorldScenario101 implements Runnable {



    /**
     * @param args
     */
    public static void main(String[] args) {
     SwingUtilities.invokeLater(new HelloWorldScenario101());
    }



    public HelloWorldScenario101() {
     //
    }



    @Override
    public void run() {

     this.frame = new JFrame();
     this.panel = new JSGPanel();
     this.text = new FXText();
     this.paint = new Color(255, 0, 0, 255);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setTitle("Hello World");
     frame.add(this.panel);
     frame.setContentPane(this.panel);
     scene = new SGGroup();
     this.text.setText("Hello World");
     this.text.setFillPaint(this.paint);
     this.text.setLocation(new Point2D.Float(10, 10));
     this.scene.add(this.text);
     this.panel.setScene(scene);
     frame.pack();
     frame.setLocationByPlatform(true);
     frame.setVisible(true);

    }



    private JFrame frame;

    private JSGPanel panel;

    private SGGroup scene;

    private FXText text;

    private Paint paint;



}
facildelembrar