Chapter 11. Best of both worlds: using JavaFX from Java

 

This chapter covers

  • Mixing JavaFX into Java programs
  • Calling JavaFX Script as a scripting language
  • Defining our Java app’s Swing UI in JavaFX
  • Adding JavaFX to the Java classpath

In previous chapters we saw plenty of examples demonstrating Java classes being used from JavaFX Script. Now it’s time for an about-face; how do we call JavaFX Script from Java?

Because JavaFX Script compiles directly to JRE-compatible bytecode, it might be tempting to assume we can treat JavaFX-created classes in much the same way we might treat compiled Java classes or JARs. But this would be unwise. There are enough differences between the two languages for assumption making to be a dangerous business. For example, JavaFX Script uses a declarative syntax to create new objects; any constructors in the bytecode files are therefore a consequence of compiler implementation, not the JavaFX Script code. We can’t guarantee future JavaFX Script compilers will be implemented the same way; constructors present in classes written by one JFX compiler might vanish or change in the next. So in this chapter we’ll look at how to get the two languages interacting in a manner that doesn’t depend on internal mechanics.

11.1. Different styles of linking the two languages

11.2. Adventures in JavaFX Script

11.3. Adding FX to Java

11.4. Summary

sitemap