JavaFX API

JavaFX API is used for creating GUI rich desktop applications also called as Rich internet applications (RIAs). It is a set of Java libraries designed to enable you to create and deploy rich client applications that have rich UI controls. It can be used to create graphical user interfaces for many platforms.

I will use BlueJ Editor to create a sample class. Create a new Java Class Main and choose the option JavaFX class.

Sample JavaFX class

public class Main extends Application
{
    // We keep track of the count, and label displaying the count:
    private int count = 0;
    private Label myLabel = new Label("0");

    @Override
    public void start(Stage stage) throws Exception
    {
        // Create a Button or any control item
        Button myButton = new Button("Click to Increase Count");

// Create a new grid pane
        GridPane pane = new GridPane();
        pane.setPadding(new Insets(10, 10, 10, 10));
        pane.setMinSize(300, 300);
        pane.setVgap(10);
        pane.setHgap(10);

//set an action on the button using method reference
        myButton.setOnAction(this::buttonClick);

// Add the button and label into the pane
        pane.add(myLabel, 1, 0);
        pane.add(myButton, 0, 0);

// JavaFX must have a Scene (window content) inside a Stage (window)
        Scene scene = new Scene(pane, 300,100);
        stage.setTitle("Simple JavaFX Application");
        stage.setScene(scene);

// Show the Stage (window)
        stage.show();
    }

    /**
     * This will be executed when the button is clicked
     * It increments the count by 1
     */
    private void buttonClick(ActionEvent event)
    {
        // Counts number of button clicks and shows the result on a label
        count = count + 1;
        myLabel.setText(Integer.toString(count));
        System.out.println("You have clicked the button");
    }
}

Application class

The Application class is the primary class for the JavaFX applications. It is the entry point for the application. for example (start methods ).

public class SampleFXApp extends Application {

    public void start(Stage stage) {

   .

   .

   .

    }

}

The Scene , Stage are top level classes that acts like containers for all content in a scene. Containers hold other class objects ( like Buttons , Menu, controls etc)

To run the application, right click on the class and choose Run JavaFX Application.