2

I want to create an applet using Javafx Scene Builder. I know the traditional way of creating an applet but then I recently started working with JavaFx and I am in love with the way you can create awesome User Interface designs using Scene Builder and then write neat customized code. So, far I can create desktop programs using JavaFx but now I want to write small utility programs and embed it on a browser. I honestly dislike the idea of writing codes to create the U.I. of an applet, so I was thinking if there was a way to create an applet using Javafx.

P.S - I am new to Stackoverflow. So, please forgive any mistakes I make.

Shenaaz
  • 23
  • 4
  • 2
    Look at non-applet browser deployment http://docs.oracle.com/javafx/2/deployment/deployment_toolkit.htm and http://stackoverflow.com/questions/19102000/javafx-can-it-really-be-deployed-in-a-browse – Joop Eggen Oct 03 '16 at 11:51

2 Answers2

3

Applets are a dead technology, so you should forget them altogether. Support for applets has been removed at least in the newest Chrome browser, and most likely others too.

It's not possible to create applets via JavaFX either.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 2
    It's still technically possible to create JavaFX applets, and Oracle still provides [tools](http://docs.oracle.com/javase/8/docs/technotes/guides/deploy/) to enable you to do so. However, you are [completely correct](http://openjdk.java.net/jeps/289) that this is a dead technology and you should look for alternative approaches. – James_D Oct 03 '16 at 11:01
  • @James_D My mistake. I was under the impression that JavaFX was never extended to applets, as they've been in a deprecated state for quite some time. – Kayaman Oct 03 '16 at 11:13
3

It can be done thanks to a JFXPanel which allows to embed a JavaFX component into a Swing component and from a Swing component you can easily create an Applet.

Here is an example that draws a Java FX Rectangle into an Applet:

public class Main extends Applet {

    private void initAndShowGUI() {
        // This method is invoked on Swing thread
        final JFXPanel fxPanel = new JFXPanel();
        fxPanel.setPreferredSize(new Dimension(100, 100));
        add(fxPanel);
        setVisible(true);
        Platform.runLater(
            new Runnable() {
                @Override
                public void run() {
                    initFX(fxPanel);
                }
            }
        );
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on JavaFX thread
        final Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene() {
        final Rectangle rectangle = new Rectangle(100.0, 100.0);
        rectangle.setFill(Color.BLACK);
        return new Scene(new VBox(rectangle));
    }

    @Override
    public void init() {
        SwingUtilities.invokeLater(
            new Runnable() {
                @Override
                public void run() {
                    initAndShowGUI();
                }
            }
        );
    }
}

However Applets are outdated and will be removed soon so if you can avoid using them, just don't use them. Alternatively you can use the Deployment Toolkit library to embed your JavaFX application in a web page or launch it from a browser, more details about this approach here.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • But you can create applets with JavaFX without embedding them in Swing. You should avoid mixing the two toolkits unless it's absolutely necessary. – James_D Oct 03 '16 at 12:28
  • @James_D if so please provide an answer then, I can propose only what I know if there is a better approach I would be happy to know it – Nicolas Filotto Oct 03 '16 at 12:30
  • The correct answer was provided by @Kayaman: Applets will be deprecated in Java 9 and you should avoid using them entirely (either with Swing or with JavaFX). I already provided a [link](http://docs.oracle.com/javase/8/docs/technotes/guides/deploy/) below that question to the tools to create a JavaFX Applet for those who insist on ignoring the [official position](http://openjdk.java.net/jeps/289). – James_D Oct 03 '16 at 12:33
  • @James_D I already checked your link I can't see anything specific that explain how to do a JavaFX Applet, so do you have a more specific link to provide? Unless you meant this http://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html? IMHO indicating that it is outdated should be more a comment or at worse a NB in an answer, rather than a real answer. Indeed maybe the OP doesn't have the choice so knowing that it is outdated doesn't help much – Nicolas Filotto Oct 03 '16 at 12:49
  • Every chapter in that guide covers all four [execution modes](http://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deploy_overview.html#BABCGDJF) (1. standalone program, 2. Java Web Start, 3. Embedded in a browser, i.e. applet, and 4. self-contained application). I **completely** disagree about providing an answer that encourages the use of something that is explicitly deprecated: that is akin to misinformation imho. – James_D Oct 03 '16 at 12:55
  • @James_D I added a note indicating to not use it if possible, feel free to down vote if my answer is not correct for you, at least it will be a down vote with a comment which is still appreciable – Nicolas Filotto Oct 03 '16 at 13:02