Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

What is the difference between Java and JavaFX?

1 min read

javafx_logo_color_1In my last article I explained the difference between HTML5 and SVG. Since then, another request has been for a simple answer to the question ‘What is the Difference between Java and JavaFX?’.

Here goes my attempt in 5 steps…

1. First we had Java, the Computer language

In the 1990s, Sun Microsystems created a new Computer Language called Java. It proved very popular because it made it easy to write code which focused on the problem, not on the computer platform. Lots of issues which other languages had (memory allocation and leaks, speed, porting to other devices, confusing syntax) simply did not exist in Java. So Java became a very popular language, especially on servers.

2. Adding in Applications with Graphical User Interfaces

Many users wanted to created Java programs with a graphical user interfaces. So Java gained a set of libraries (Swing) as part of Java. This allowed you to create applications in Java with user interfaces, but many developers felt Swing was over-complex and Java on the desktop never really took off as it did on the server.

3. Initial attempts to Improve with JavaFX 1.0

Sun Microsystems tried several ways to make it easier to create Java applications. One of these was a scripting language (so not Java but could be used with Java) called JavaFX 1.0 which tried to compete with Adobe Flash and Microsoft Silverlight. This allowed users to build much more complex user Interfaces (which could also include advanced timelines for events) than was possible in Swing.

It did not really take off (but managed to be the theme of several JavaOne conferences in the late 2000s). JavaFX was not Java (so it never really caught on with Java developers who did not want to learn yet another language) and it offered no real compelling advantage to non-Java developers.

4. Oracle reorganises with JavaFX 2 and above

When Oracle acquired Sun Microsystems, there was a major review of all activities. They killed off JavaFX as a scripting language but added the functionality into Java (with Java commands) and enhanced it as the new way to develop user interfaces.

While there is no longer a Scripting language, JavaFX can still be written as either Java commands or as XML commands (FXML) which Java can load. This means that you can separate the User interface from the code, and there is a nifty JavaFX designer called SceneBuilder to create layouts in FXML.

Java continues to contain the Swing library (lots of programs still use it), and Swing and JavaFX can be used together. But for writing new Java applications, JavaFX offers a much simpler way to create desktop applications. In JavaFX you can write more powerful applications with much less code. 🙂

So you can think of JavaFX as a modern part of Java which makes it easy to write desktop applications. It also works really nicely on the Raspberry Pi and other embedded devices where you want to create an application for the user to interact with.

5. Getting started with JavaFX

There are lots of JavaFX examples built in to NetBeans IDE which is a really good place to start. Create a project and tinker…

javafx examples
Choose a JavaFX Project in NetBeans as a Start…

 

 

 



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Creating a WordPress Plugin Part 3: Adding Shortcodes

Recently, I’ve been working on a proof of concept project which integrates our JPDF2HTML5 web service with the popular blogging platform WordPress. This series of...
Simon Lissack
1 min read

Creating a WordPress Plugin Part 1: Adding the Admin…

WordPress is one of the most popular content management systems on the internet. Created using PHP it can handle all things blogging to commercial websites. In...
Simon Lissack
2 min read

4 Replies to “What is the difference between Java and JavaFX?”

  1. Is there a way to use the functions provided by the javafx libraries to use in an existing regular, console based Java application? For instance in my console app i need to add sound from mp3 which i have to start playing on an arbitrary position in the file.

      1. Why using Swing? I did some tests but have troubles with static MediaPlayer object:


        package be.saleconix;

        import java.net.URL;
        import java.util.concurrent.CountDownLatch;

        import javafx.application.Application;
        import javafx.embed.swing.JFXPanel;
        import javafx.scene.media.Media;
        import javafx.scene.media.MediaPlayer;
        import javafx.stage.Stage;
        import javafx.util.Duration;

        class AudioPlayer {
        URL resource;
        Media media;
        static MediaPlayer mediaPlayer;

        public AudioPlayer(String filename) throws NullPointerException, InterruptedException {
        new JFXPanel(); // initializes JavaFX environment
        this.resource = getClass().getResource("/" + filename);
        this.media = new Media(resource.toString());
        this.mediaPlayer = new MediaPlayer(media);
        }

        void play() {
        mediaPlayer.setOnReady(()->{
        mediaPlayer.play();
        });
        }

        void stop() {
        mediaPlayer.stop();
        }

        void playPosition(double position) {

        mediaPlayer.setStartTime(Duration.millis(position));
        mediaPlayer.setOnStopped(()->{
        mediaPlayer.play();
        });
        mediaPlayer.stop();
        //mediaPlayer.setOnReady(()->{

        //});
        }

        }

        public class Main {
        public static void main(String[] args) {
        try {
        AudioPlayer audioPlayerA = new AudioPlayer("test.mp3");
        System.out.println("Play the media.");
        audioPlayerA.play();
        System.out.println("Waiting 5s ...");
        Thread.sleep(5000);
        // System.out.println("Play the media.");
        // AudioPlayer audioPlayerB = new AudioPlayer("test1.mp3");
        // audioPlayerB.play();
        System.out.println("Now playing at 10s");
        audioPlayerA.playPosition(10000);
        System.out.println("Waiting 5s ...");
        Thread.sleep(5000);
        System.out.println("Stopping");
        audioPlayerA.stop();
        } catch (NullPointerException e) {
        System.out.println("Cannot create audio player, check filename.");
        } catch (InterruptedException e) {
        System.out.println("Thread interrupted: " + e);
        }
        }
        }

  2. This is a working demo:

    package be.saleconix;

    import java.net.URL;

    import com.sun.javafx.application.PlatformImpl;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    import javafx.util.Duration;

    class AudioPlayer implements Runnable {
    private URL resource;
    private Media media;
    private MediaPlayer mediaPlayer;
    private boolean ready = false;
    private Thread t;
    private String threadname;
    private static Integer numberOfActiveThreads = 0;

    /**
    * Start a mediaplayer thread
    * @param filename
    * @param threadname
    * @throws NullPointerException
    * @throws InterruptedException
    */
    public AudioPlayer(String filename, String threadname) throws NullPointerException, InterruptedException {
    if (numberOfActiveThreads == 0) {
    PlatformImpl.startup(()->{}); // initialize JavaFX environment
    System.out.println("The JavaFX environment has been started");
    }
    this.threadname = threadname;
    resource = getClass().getResource("/" + filename);
    media = new Media(resource.toString());
    t = new Thread(this, threadname);
    t.start();
    numberOfActiveThreads ++;
    }

    public void run() {
    mediaPlayer = new MediaPlayer(media);
    mediaPlayer.setOnReady(() -> { // register a callback and let it deregister itself after execution
    System.out.println("The mediaplayer [" + threadname + "] has been started");
    ready = true;
    mediaPlayer.setOnReady(null);
    });
    while (!t.isInterrupted());
    System.out.println("The mediaplayer [" + threadname + "] has been terminated");
    }

    String getThreadname() {
    return threadname;
    }

    /**
    * Plays the sound from the start
    * @throws InterruptedException
    */
    void play() throws InterruptedException {
    play(0);
    }

    /**
    * Plays the sound at a specific position in ms
    * @throws InterruptedException
    * @throws IllegalThreadStateException
    */
    void play(double position) throws InterruptedException, IllegalThreadStateException {
    int i = 500;
    while (!ready && i > 0) {
    Thread.sleep(1);
    i--;
    }
    if (i > 0) {
    mediaPlayer.setStartTime(Duration.millis(position));
    if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
    mediaPlayer.setOnStopped(()->{
    mediaPlayer.play();
    mediaPlayer.setOnStopped(null);
    });
    mediaPlayer.stop();
    } else {
    mediaPlayer.play();
    }
    } else {
    throw new IllegalThreadStateException("The mediaplayer is not ready after 500ms timeout");
    }
    }

    /**
    * Stop the mediaplayer thread
    */
    void stop() {
    mediaPlayer.setOnReady(null);
    mediaPlayer.setOnStopped(null);
    mediaPlayer.stop();
    numberOfActiveThreads --;
    t.interrupt();
    if (numberOfActiveThreads == 0) {
    PlatformImpl.exit(); // exit the JavaFX environment
    System.out.println("The JavaFX environment has been exited");
    }
    }

    }

    public class Main {
    public static void main(String[] args) {
    try {
    AudioPlayer audioPlayerA = new AudioPlayer("test.mp3", "audioplayerA");

    System.out.println("Play audioplayerA from start");
    audioPlayerA.play();

    System.out.println("Waiting 10s ...");
    Thread.sleep(10000);

    System.out.println("Play audioplayerA at position 20s");
    audioPlayerA.play(20000);

    System.out.println("Waiting 5s ...");
    Thread.sleep(5000);

    AudioPlayer audioPlayerB = new AudioPlayer("test1.mp3", "audioplayerB");

    System.out.println("Play audioplayerB from start.");
    audioPlayerB.play();

    System.out.println("Waiting 20s ...");
    Thread.sleep(20000);

    System.out.println("Stopping audioplayerA");
    audioPlayerA.stop();

    System.out.println("Stopping audioplayerB");
    audioPlayerB.stop();

    } catch (NullPointerException e) {
    System.out.println("Cannot create audio player, check filename.");
    } catch (InterruptedException e) {
    System.out.println("Thread interrupted: " + e);
    } finally {
    System.out.println("Main thread has been exited");
    }
    }
    }

Comments are closed.