JavaFX, the new platform for creating and delivering desktop applications

Spread the love

Introduction

JavaFX is a software platform for creating and delivering desktop applications, as well as rich Internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future. JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linux, and macOS.

JavaFX
JavaFX

Similar to Eclipse SWT, It’s written for the Desktop and replaces Swing and AWT. Next to the Desktop, JavaFX can also be used to be used with Webbrows.

The code below shows how a Window named ‘HelloWorld’ with a button ‘HelloWorld’ named  is created. When the button is clicked, the console prints Hello World.

package javafxtuts;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFxTuts extends Application {
    public JavaFxTuts()
    {
        //Optional constructor
    }
    @Override
    public void init()
    {
         //By default this does nothing, but it
         //can carry out code to set up your
         //app.
         //It runs once before the start
         //method and after the constructor.
    }
    
    @Override
    public void start(Stage primaryStage) {
        // Create the Java button
        final Button button = new Button();
        // Setting text to button
        button.setText("Hello World");
        // Registering a handler for button
        button.setOnAction((ActionEvent event) -> {
            // Printing Hello World! to the
            // console
            System.out.println("Hello World!");
        });
        // Please note that this Synax is a
        // Lambda Expression 
        // Initializing the StackPane class
        final StackPane root = new StackPane();
        // Adding all the nodes to the StackPane
        root.getChildren().add(button);
        // Creating a scene object
        final Scene scene = new Scene(root, 300,
         250);
        // Adding the title to the window
        // (primaryStage)
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        // Show the window(primaryStage)
        primaryStage.show();
    }
    @Override
    public void stop()
    {
        //By default this does nothing
        //It runs if the user clicks the 
        //go-away button
        //closing the window or if 
        //Platform.exit() is called.
        //Use Platform.exit() instead of
        //System.exit(0).
        //This is where you should offer to save
        //any unsavedstuff that the user 
        //may have generated.
    }
 
    /**
     * Main function that opens the "Hello
     * World!" window
     * Note that Main starts the execution of the
     * Application, just as in any regular 
     * Java Application.
     * @param args the command line arguments
     */
        public static void main(final String[] 
        arguments) {
    // The actual start takes place with the
    //  launch(arguments);
    //  launch(arguments); Command:
        launch(arguments);
    }
}
References and Tutorials

There exist many on the web:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.