The Enterprise Side of JavaFX: The Missing Part

Write some Java logic

Double click MainSwing.java in the SomeApp-app project. Make sure your code reads something like this:

.
package com.martinandersson.something;

import javax.ejb.EJB;

/**
 * @author Martin Andersson
 */
public class MainSwing
{
    @EJB
    public static RemoteSomething remoteEJB;

    public static void main(String[] args)
    {
        MainFX.main(args);
        
        // ..or alternatively:
        // javafx.application.Application.launch(MainFX.class, args);
    }
}
.

Open somethingController.java file and write this:

.
package com.martinandersson.something;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
 * @author Martin Andersson
 */
public class somethingController implements Initializable
{
    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event)
    {
        label.setText(MainSwing.remoteEJB.saySomething("Remoting is awesum."));
    }

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        ; // Empty
    }
}
.

Next up, write some code in your MainFX.java file:

.
package com.martinandersson.something;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * @author Martin Andersson
 */
public class MainFX extends Application {

    @Override
    public void start(Stage stage)
    {
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("something.fxml"));
        } catch (IOException ex) {
            Logger.getLogger(MainFX.class.getSimpleName()).log(Level.SEVERE, "Failed to load something.fxml!", ex);
            System.exit(1);
        }

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application. main() serves only as a fallback in
     * case the application can not be launched through deployment artifacts, e.g., in IDEs with limited FX support.
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
.

Please take a minute to figure out exactly how the previous code snippet differs from the code NetBeans produced. NetBeans added a throws Exception clause to the MainFX#start(Stage) method. Once you remove this clause you will find that the added clause effectively masked the checked exception I had to wrap in a try-catch block at the root (pun intended) of all evil. Adding a throw clause for such a low level exception type to your method declaration will mask all uncaught checked exceptions from all nested method invocations. Indeed it will simply put it brake all good uses we have for dealing with exceptions. It doesn’t even add anything since all methods in Java still throws Exception implicitly. Adding this explicitly does not do anything else than twiddle the mind of newcomers. There are only two exceptions to the rule that you should never add a throws Exception clause to a method declaration. You can do so if you feel that the thrower represents the topmost layer of an application like a GUI in this example (I still this practice anti-pattern) and it is even good for you to do so in a JUnit test case annotated in a particular way to test what type of exception your test case threw.

Okay, back to the tutorial! We can leave something.fxml as is. If you ever want to get serious with JavaFX, I bet you want to have a look at this blog post and see how to integrate Oracle’s Scene Builder with your IDE.

11 thoughts on “The Enterprise Side of JavaFX: The Missing Part

  1. mauro

    hi your blog it is very very interessant.
    But how you resolve the problem with the call at remote ejb or application client on a glassfish behind a firewall ?

    For example i would download and use the appclient and the glassfish it is on a another macheine extenal at the internal net but on intenet .
    go at :
    https://www.java.net//forum/topic/glassfish/glassfish/ejb-remote-deployed-gf-31-behind-nat-unaccessible
    for view the problem .
    tanl you very much for your attention
    Mauro

    Reply
    1. martinandersson.com Post author

      I have no solution to your problem. Actually I haven’t read your URL:s, just looked over them briefly. However, all kinds of remoting is always troublesome if you’re behind a firewall. HTTP wired web services has no issue since they talk on port 80, almost always unblocked, and I believe this is the chief reason they have gained so much in popularity. So the moment a firewall gives you a headache, just expose the EJB as a web service. There is nothing stopping you from combining @WebService with @Stateless.

      Reply
  2. Pingback: JavaFX links of the week, July 22 // JavaFX News, Demos and Insight // FX Experience

  3. AFX

    Grymt martin! Keep it up!
    Nice kodat, och nu ska jag ta och kolla igenom resten av bloggen;) Man blir ju sugen på att börja koda själv fan. Verkar skitkul.

    Reply
  4. R

    Hi. Thanks for this blog post. It is really helpful. I’ve managed to run it inside the Netbeans. Unfortunately I’ve failed to do it outside the Netbeans. I used Glassfish appclient. I have the following errors……


    Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:212)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.joy.azura.Sly.main(Sly.java:23)
    Caused by: java.lang.ClassNotFoundException: javafx.application.Application
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:212)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more

    In line 14 (Sly.java:23) my code is

    javafx.application.Application.launch(SlyFX.class, args);

    I’m able to execute it inside the Netbeans. Thus, I think there is a way to do it outside of the Netbeans.

    Reply
    1. martinandersson.com Post author

      Could it be that the Application Client Container (ACC) cannot find your JavaFX runtime? And when you say “outside of NetBeans”, I hope you mean using Java Web Start? Please review page 8 and see if it helps. And be sure to upgrade your JDK installed if it is anywhere below 1.7.0_6.

      Reply
      1. R

        Thanks for your prompt reply. I was using the old next to obsolete method. “asadmin …get-client-stubs… appclient …” so you will have the lighter Glassfish on the client’s computer. Then to execute the Client side type “appclient.bat -jar MyApp-Client.jar” in command line. I have a fat client which needed to work offline. Fortunately requirements have changed. Now I have decided to switch to RESTful Web Service. That one has much better option. Thanks again.

        Reply
  5. Jeff

    Hi

    Is this still required/workable in Java/JavaFX 8, Glassfish 4.1 and netbeans 8.02?

    I quickly run through this tutorial in them and could not get it to work. I get a non specific security violation when the webstart finishes downloading and attempts to run it.

    Thanks!

    Reply
  6. Pingback: Java desktop links of the week, July 22 – Jonathan Giles

Leave a Reply

Your email address will not be published. Required fields are marked *