These are simple step by step code snippets to allow you learn JavaFX.

Example 1: Togglebutton

Here are the code snippets:

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:

  • ToggleButtonExample.java
  • ToggleButtonFontExample.java
  1. In your editor or IDE, create a file known as ToggleButtonExample.java.
  2. Then add the following code:

(a). ToggleButtonExample.java

So guys let;s go ahead and add some imports into our project. We add imports using the import keyword:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

The next step is to extend our class as shown below:

public class ToggleButtonExample extends Application {

Our class will have the following methods:

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

For you to be able to execute this class you need to assign it a main method using the following definition:

    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) {

        //First, simple example of creating a MenuButton
        ToggleButton toggleButton = new ToggleButton("Toggle This!");

        toggleButton.setOnAction((event) -> {
            System.out.println("Toggle button clicked");
            String buttonText = toggleButton.isSelected() ? "On" : "Off";
            toggleButton.setText(buttonText);
        });

        VBox vbox = new VBox(toggleButton);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.togglebutton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX MenuButton - and prints a text when a menu item from the button is selected.
 */
public class ToggleButtonExample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        //First, simple example of creating a MenuButton
        ToggleButton toggleButton = new ToggleButton("Toggle This!");

        toggleButton.setOnAction((event) -> {
            System.out.println("Toggle button clicked");
            String buttonText = toggleButton.isSelected() ? "On" : "Off";
            toggleButton.setText(buttonText);
        });

        VBox vbox = new VBox(toggleButton);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

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

(b). ToggleButtonFontExample.java

So guys let;s go ahead and add some imports into our project. We add imports using the import keyword:

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

The next step is to extend our class as shown below:

public class ToggleButtonFontExample extends Application {

Our class will have the following methods:

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

For you to be able to execute this class you need to assign it a main method using the following definition:

    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) {

        //First, simple example of creating a MenuButton
        ToggleButton toggleButton = new ToggleButton("Toggle This!");

        Font arialFontBold36  = Font.font("Arial", FontWeight.BOLD, 36);

        toggleButton.setFont(arialFontBold36);

        toggleButton.setOnAction((event) -> {
            System.out.println("Toggle button clicked");
            String buttonText = toggleButton.isSelected() ? "On" : "Off";
            toggleButton.setText(buttonText);
        });

        VBox vbox = new VBox(toggleButton);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.togglebutton;

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

/**
 * Shows a simple JavaFX MenuButton - and prints a text when a menu item from the button is selected.
 */
public class ToggleButtonFontExample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        //First, simple example of creating a MenuButton
        ToggleButton toggleButton = new ToggleButton("Toggle This!");

        Font arialFontBold36  = Font.font("Arial", FontWeight.BOLD, 36);

        toggleButton.setFont(arialFontBold36);

        toggleButton.setOnAction((event) -> {
            System.out.println("Toggle button clicked");
            String buttonText = toggleButton.isSelected() ? "On" : "Off";
            toggleButton.setText(buttonText);
        });

        VBox vbox = new VBox(toggleButton);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        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