JavaFX – Some FAQ

My colleague Vaibhav sent me a list of queries he got from forums. Here is an attempt to respond to them..

Can I embed JavaFX script into an HTML page, the way I can embed Java source code into a JSP page?
Java embedded in JSP page gets executed on server side. JavaFX is client side technology and gets executed on client system (similar to JavaScript). So you can embed JavaFX program in JSP as an Applet or using javafx script tag.

Can I include JavaFX code within a JavaScript function?
You cannot directly embed JavaFX code inside JavaScript. But you can very well integrate and interact with JavaScript using Java LiveConnect support.
Refer to this sample and post for more information.

Can I mix JavaFX and JavaScript together with php and jsp tags on an HTML page? What happens when I mix these technologies?
Yes. As mentioned in response to above question, you can mix JavaFX and JavaScript and interact between each other, share/update data. JavaFX and JavaScript executes on client side and PHP/JSP gets executed on server side.

Can I send HTTP commands directly to a server and then fetch and display the results in my browser?
How is JavaFX intended to interface with servlets?
How do I fetch data from a server and then immediately update a local HTML page display using JavaFX?
How can I mimic Ajax functionality?

Yes. Refer to this sample and post. JavaFX application read the RSS feed and parse the data. The data is then updated in the HTML page using JavaScript. You can also interface with any REST based service using HttpRequest and PullParser APIs.
There are many samples which demonstrates this –

Can I write a complete (but simple) server application that connects to the web and can recognize, parse and respond to simple messages from clients across a network?
Yes. JavaFX provides many APIs such as HttpRequest and PullParser which allow to read and parse XML/JSON data. You may also use any of existing Java technology in JavaFX for such interaction.

I can see how JavaFX instantiates a Swing object from a core class in the JRE. Can it also instantiate a custom object that I have written? How can I do this? What are the security implications?

You can encapsulate any existing Swing component using SwingComponent class so that it can be used as Node in JavaFX. SwingComponent.wrap(jComponent: javax.swing.JComponent) wraps an existing Swing component and returns SwingComponent instance. It just needs to be an instance of JComponent. Refer to this sample – ExercisingSwing. Security implications depends on the action performed by the component. Example: If its an unsigned Applet, access to system resource may be restricted. Those security restrictions will be handled by Java sandbox.

How do I interface a JavaFX script to a relational database?
One of power of JavaFX is that you can use any of the APIs provided by Java. So you can use all your existing database integration code, the same way as you do it in Java. Yes if you use any JavaSE specific APIs the application may not be portable to Mobile platform. So in such cases you may have to encapsulate the back end interaction using other means such as REST.
Refer to these posts:

Ajax pages have problems supporting browser ‘BACK’ button behavior. Can JavaFX overcome these problems and present users with the page-back behavior they have come to expect?
There is no built in back button support in JavaFX.

Can Google discover text intended to be displayed by a JavaFX-scripted Swing application?
No. JavaFX is deployed in compiled class file format. Google cannot directly read this data. Google can find you application if the HTML page in which this Applet is embedded has sufficient data to search and find this application.

How can I run JavaFX outside my browser as a local system scripting language?
You can deploy the JavaFX application in different formats – Standalone, JavaWebStart, Browser (and drag out of
browser) and Mobile (if the application use just Common profile APIs). The deployment mode can be chosen based on your requirement. Its not tied to the application code.

Is it possible to include a JavaFX Scene in a regular Swing application?
Answer in

digg_skin = ‘compact’;
digg_window = ‘new’;