This discussion is archived
5 Replies Latest reply: Jan 18, 2013 4:36 PM by 985606 RSS

Load/Unload FXML like plugins

985606 Newbie
Currently Being Moderated
Hi, I'm a programmer coming from .NET, using technologies like WPF and DirectX among others... One of the reasons for leaving .NET is the mismanagement and inability to release usercontrols in a logical and easy way. Each of these usercontrols in the world. NET are .dll files that are instantiated via reflection and loaded into the main window as a plugin. Let's look at each of these plugins as a multimedia application with video, graphics, sound, or any other resources that can be loaded and later unloaded after serving its purpose.

My question is, this is possible in JavaFX? I imagine a Stage that loads each of these plugins as FXML, release them and replace it with a new plugin when required. It's like media player that plays FXML's, instead of songs or movies.

Again, I'm a Java newbie and my only reason to learn it is that if it works as I think, JavaFX will have no competitor.

Thanks for your help,

Roel Weapons
  • 1. Re: Load/Unload FXML like plugins
    Narayan Pro
    Currently Being Moderated
    HI user,
    Well I am not quiet familiar with the .NET but Lets make clear about this vision of FXML.
    Firstly The FXML is nothing but just the XML structure of the JavaFX UI. You can easily plug out and plug in the FXML as you need. The JavaFX Loads the FXML at runtime and parses the XML to Java Language which displays the JavaFX Controls as per your need. You cannot make any conditional statement inside the XML. JavaFX has the good pattern for all designer and coders to work easily.
    To know more about FXML : Mastering FXML


    Thanks
  • 2. Re: Load/Unload FXML like plugins
    985606 Newbie
    Currently Being Moderated
    Narayan, Thanks for your quick reply. Let me understand something, JavaFX takes FXML file and runs in real time in the style of an interpreted language? For the tests I've done, if it has an associated FXML controller, it must be compiled.

    How I can do to load a FXML that haves a controller but both are in a different path to the application that invokes it? I understand that using FXMLLoader.load (getClass (). GetResource ()), we are invoking a resource within the project prepackaged, or am I wrong? If so, what is the best practice to load drivers that are referenced FXML and what is the way to unload them?

    Thanks and Regards,

    Roel Armas
  • 3. Re: Load/Unload FXML like plugins
    James_D Guru
    Currently Being Moderated
    982603 wrote:
    Narayan, Thanks for your quick reply. Let me understand something, JavaFX takes FXML file and runs in real time in the style of an interpreted language?
    Sort of. When the FXML file is loaded, objects are instantiated based on the content of the FXML file and will exist in memory. The FXMLLoader.load() method returns the object representing the root of the fxml document (which will probably have references to other objects, etc). Some objects may also be injected into the controller (so the controller has references to them). Once the FXML file has been parsed, methods on the objects which were instantiated are invoked in the normal fashion (i.e. the FXML file is no longer needed). So you can regard the FXML file as being parsed once, rather than "interpreted" in the traditional sense.
    For the tests I've done, if it has an associated FXML controller, it must be compiled.

    How I can do to load a FXML that haves a controller but both are in a different path to the application that invokes it? I understand that using FXMLLoader.load (getClass (). GetResource ()), we are invoking a resource within the project prepackaged, or am I wrong?
    The FMXLLoader.load(...) method takes a parameter of type URL. So you can pass any URL object in here: see http://docs.oracle.com/javase/7/docs/api/java/net/URL.html The getClass().getResource(...) method, at least on an intuitive level, constructs a URL which represents loading the resource from "the same place this class was loaded". So if you're running directly from the file system (which you shouldn't be, for a JavaFX application), it will be a URL pointing to the file system. If you're running from a jar file, it will look for the resource in the jar file. (This is something of a simplification, but it a good starting point. The complete details are at http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String) [You probably need to edit the link in your browser to include "(java.lang.String)", which is intended to be part fo the url.]).
    If so, what is the best practice to load drivers that are referenced FXML and what is the way to unload them?
    I'm not sure what you mean by "drivers" here. The best practice to load FXML files is to bundle them with your application, and load them using getClass().getResource(...). If you are building an application in which the FXML would be loaded from the file system, you could call FXMLLoader.load(new URL("file:///path/to/my/file.fxml")). (But how would you ensure the FXML file was there?) If you were (for some reason) loading an FXML file from a web server, you could call FXMLLoader.load(new URL("http://my.server.com/my/file.fxml")).
    >
    Thanks and Regards,

    Roel Armas
  • 4. Re: Load/Unload FXML like plugins
    jsmith Guru
    Currently Being Moderated
    JavaFX takes FXML file and runs in real time in the style of an interpreted language?
    I'd think of the FXMLLoader as more like a web browser loading HTML.

    In the HTML world, the browser will parse the HTML and then create a document object model based on the HTML.
    In the FXML world, the FXMLLoader will parse the HTML and create a JavaFX scenegraph object model based on the FXML.

    If the FXML features inline JavaScript scripts, then the execution model is pretty much like HTML with JavaScript. For a browser, the JavaScript is executed on a JavaScript VM such as Chrome V8. For FXML, the JavaScript will be executed by a JavaScript engine bundled with the Java System, e.g. a Rhino derivative (http://en.wikipedia.org/wiki/Rhino_%28JavaScript_engine%29) or nashorn (http://en.wikipedia.org/wiki/Nashorn_%28JavaScript_engine%29).

    If the FXML features a separate controller in Java code, then that Java code is pre-compiled to java byte code - which is an intermediate representation between source code and machine code. The java byte code is executed by a Java Virtual Machine at a speed that (in most cases) for all intents and purposes is comparable to machine code execution.

    For me, the ability to simply and easily choose and use from FXML both a scripting language and a compiled language from any of the many available languages executable on the JVM is one of the major technical advantages of JavaFX over HTML. For example you can write scripting and controller logic in Ruby if you liked: http://stackoverflow.com/questions/13831708/using-fxml-with-jruby.
    How I can do to load a FXML that haves a controller but both are in a different path to the application that invokes it?
    The FXMLLoader load method has a url paramater to specify the location of where the source FXML file originates - it works in the same way as entering a URL into your web browser's location bar.
    http://docs.oracle.com/javafx/2/api/javafx/fxml/FXMLLoader.html#FXMLLoader%28java.net.URL%29
    I understand that using FXMLLoader.load(getClass().getResource()), we are invoking a resource within the project prepackaged, or am I wrong?
    Pretty much correct. getClass().getResource() will use the originating codebase of the classloader, which will generally be the source jar for the application. But you don't have to load a resource like this, you can use any url you like (within the Java security sandbox which currently doesn't really apply to FXML anyway as FXML won't execute within any kind of security sandbox as of JavaFX 2.2).
    If so, what is the best practice to load drivers that are referenced FXML and what is the way to unload them?
    With FXML, you can define your own components and tags to extend the language; i.e. it does not have a fixed tagset. In addition to the extensive set of components provided by the JavaFX system, you can easily define your own custom components. Perhaps these are like your notion of drivers and plugins (which I have to admit is hard for me to understand)?
    http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html#custom_components

    So that covers what happens when you load FXML. With regard to unloading FXML, there is no such explicit thing. Essentially, like other memory management tasks in Java, it's automatically handled by the Java garbage collector. The JavaFX system automatically handles low level stuff like freeing up GPU texture memory for scene graph objects which are no longer required. Java and JavaFX are essentially higher level abstractions than writing in something like C code with OpenGL and the Java and JavaFX runtimes take care of many of the mundane tasks such as garbage collection and system resource cleanup for you.

    If a custom FXML component made use of some resource such as a database connection which you wanted to explicitly release when the component is no longer used, then you could add a listener to the component's scene property. When the component is no longer part of an active scene, the listener would be triggered and you could run some code there to free up your resources (e.g. release the database connection).


    For an example of a system which dynamically loads fxml files and lets the Java system garbage collect them when they are no longer needed, see the webfx project: https://github.com/brunoborges/webfx. WebFX also provides some interesting examples of fxml without controllers.
  • 5. Re: Load/Unload FXML like plugins
    985606 Newbie
    Currently Being Moderated
    Thanks guys for your invaluable help! I'm really fascinated with this technology and very excited about JavaFX capabilities (in my case) for audiovisual purposes.

    So, as I said before, I'm coming disappointed from world .net / wpf. world. My goal is to turn a PC into a powerful multimedia player capable of playing high-definition videos, sounds, silent applications (automations for example) and multimedia applications (dynamic content, video games etc.) able to reinvent themselves depending on their context. This is possible using JavaFX, QT or WPF among others. I think that this is next step in audiovisual world.

    In tests I've done, when trying to load a fxml which is in a different path to the application that calls FXML file, compiler returns me the following error:

    "javafx.fxml.LoadException: java.lang.ClassNotFoundException: jvfx1.SampleController"

    My app haves the following line in order to load FXML file:

    URL location = new URL("file:///C:/JVFX1/build/classes/jvfx1/Sample.fxml"); // FXML project path //
    FXMLLoader fxmlLoader = new FXMLLoader(location);
    Parent root = (Parent) fxmlLoader.load(location.openStream());

    Inside each FXML project we have compiled classes directory (currently pointing to) also source directory. I guess that I have to call fxml document located at build/classes directory, rather than source directory, or am I wrong?

    Thanks and Regards guys!

    Roel Armas

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points