Welcome guys. Learn the JavaFX UI widgets using these code snippets.

Example 1: Button

Study the following code:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • ButtonDefaultAndCancelModesExample.java
  • ButtonExample.java
  • ButtonFontExample.java
  • ButtonFXMLController.java
  • ButtonFXMLExample.java
  • ButtonTransformationExample.java
  • DisabledButtonExample.java
  1. In your editor or IDE, create a file known as ButtonDefaultAndCancelModesExample.java.
  2. Then add the following code:

(a). ButtonDefaultAndCancelModesExample.java

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class ButtonDefaultAndCancelModesExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

We will add a main method to our Java class as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button("Click me!");

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        button.setCancelButton(false);
        button.setDefaultButton(false);

        Button buttonDefault = new Button("Default (OK)");

        buttonDefault.setOnAction((event) -> {
            System.out.println("Default Button clicked!");
        });

        buttonDefault.setCancelButton(false);
        buttonDefault.setDefaultButton(true);

        Button buttonCancel = new Button("Cancel");

        buttonCancel.setOnAction((event) -> {
            System.out.println("Cancel Button clicked!");
        });

        buttonCancel.setCancelButton(true);
        buttonCancel.setDefaultButton(false);

        HBox vbox = new HBox(button, buttonDefault, buttonCancel);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth(512);
        primaryStage.setHeight(512);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.button;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX Button - and prints a text when the button is clicked.
 */
public class ButtonDefaultAndCancelModesExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button("Click me!");

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        button.setCancelButton(false);
        button.setDefaultButton(false);

        Button buttonDefault = new Button("Default (OK)");

        buttonDefault.setOnAction((event) -> {
            System.out.println("Default Button clicked!");
        });

        buttonDefault.setCancelButton(false);
        buttonDefault.setDefaultButton(true);

        Button buttonCancel = new Button("Cancel");

        buttonCancel.setOnAction((event) -> {
            System.out.println("Cancel Button clicked!");
        });

        buttonCancel.setCancelButton(true);
        buttonCancel.setDefaultButton(false);

        HBox vbox = new HBox(button, buttonDefault, buttonCancel);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth(512);
        primaryStage.setHeight(512);
        primaryStage.show();
    }

}
  1. Next create another file known as ButtonExample.java.
  2. And add the following code:

(b). ButtonExample.java

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class ButtonExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

We will add a main method to our Java class as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button();

        button.setText("Click me!");

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        VBox vbox = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.button;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX Button - and prints a text when the button is clicked.
 */
public class ButtonExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button();

        button.setText("Click me!");

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        VBox vbox = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
  1. Next create another file known as ButtonFontExample.java.
  2. And add the following code:

(c). ButtonFontExample.java

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class ButtonFontExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

We will add a main method to our Java class as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("Click me!");

        Font font = Font.font("Courier New", FontWeight.BOLD, 36);

        button.setFont(font);

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        VBox vbox = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.button;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX Button - and prints a text when the button is clicked.
 */
public class ButtonFontExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("Click me!");

        Font font = Font.font("Courier New", FontWeight.BOLD, 36);

        button.setFont(font);

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        VBox vbox = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
  1. Next create another file known as ButtonFXMLController.java.
  2. And add the following code:

(d). ButtonFXMLController.java

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

We will create a simple class to host our data members and methods:

public class ButtonFXMLController {

We will create only a single method in this class:

  • void buttonClicked(Event e)

Here is the full code:

package com.jenkov.javafx.button;

import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class ButtonFXMLController {

    public Button button1 = null;
    private int   button1ClickCount = 0;

    @FXML
    public void buttonClicked(Event e){
        this.button1ClickCount++;

        String text = "Button1 clicked " + this.button1ClickCount + " times";

        System.out.println(text);

        button1.setText(text);
    }

}
  1. Next create another file known as ButtonFXMLExample.java.
  2. And add the following code:

(e). ButtonFXMLExample.java

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import java.io.File;
import java.net.URL;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class ButtonFXMLExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage) throws Exception

We will add a main method to our Java class as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) throws Exception method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();

        ButtonFXMLController controller = new ButtonFXMLController();
        loader.setController(controller);

        File fxmlFile = new File("assets/fxml/button-example.fxml");
        URL fxmlUrl = fxmlFile.toURI().toURL();
        loader.setLocation(fxmlUrl);

        VBox vbox = loader.<VBox>load();
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);

        primaryStage.setTitle("Button FXML Example");
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.button;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.File;
import java.net.URL;

/**
 * Shows a simple JavaFX Button FXML example. The Button is declared in the assets/fxml/button-example.fxml file
 */
public class ButtonFXMLExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();

        ButtonFXMLController controller = new ButtonFXMLController();
        loader.setController(controller);

        File fxmlFile = new File("assets/fxml/button-example.fxml");
        URL fxmlUrl = fxmlFile.toURI().toURL();
        loader.setLocation(fxmlUrl);

        VBox vbox = loader.<VBox>load();
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);

        primaryStage.setTitle("Button FXML Example");
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }
}
  1. Next create another file known as ButtonTransformationExample.java.
  2. And add the following code:

(f). ButtonTransformationExample.java

We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class ButtonTransformationExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

We will add a main method to our Java class as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button();

        button.setText("Click me!");

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        Scale scaleTransformation = new Scale();
        scaleTransformation.setX(3.0);
        scaleTransformation.setY(2.0);
        scaleTransformation.setPivotX(0);
        scaleTransformation.setPivotY(0);

        button.getTransforms().add(scaleTransformation);

        VBox  vbox  = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth(512);
        primaryStage.setHeight(256);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.button;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

/**
 * Shows a Button that is disabled via the call to setDisable(true)
 */
public class ButtonTransformationExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button();

        button.setText("Click me!");

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        Scale scaleTransformation = new Scale();
        scaleTransformation.setX(3.0);
        scaleTransformation.setY(2.0);
        scaleTransformation.setPivotX(0);
        scaleTransformation.setPivotY(0);

        button.getTransforms().add(scaleTransformation);

        VBox  vbox  = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth(512);
        primaryStage.setHeight(256);
        primaryStage.show();
    }

}
  1. Next create another file known as DisabledButtonExample.java.
  2. And add the following code:

(g). DisabledButtonExample.java

Like in every program we write, we will need to import functionalities into our project. Add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Extend the class as shown below:

public class DisabledButtonExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

Let’s create a main method as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button();

        button.setText("Click me!");

        button.setDisable(true);

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        VBox vbox = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.button;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Shows a Button that is disabled via the call to setDisable(true)
 */
public class DisabledButtonExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Button button = new Button();

        button.setText("Click me!");

        button.setDisable(true);

        button.setOnAction((event) -> {
            System.out.println("Button clicked!");
        });

        VBox vbox = new VBox(button);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License