In this tutorial you will learn about toolbar via simple step by step examples.

Example 1: ToolBar with Buttons Example

Let us look at a simple toolbar example written in Java Programming Language. Follow the following steps:

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:

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

(a). ToolBarExample.java

Start by adding imports:

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

Extend the Application class and invoke the launch() method inside the main() method:

public class ToolBarExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

Override the start() method, passing in aStage object:

    @Override
    public void start(Stage primaryStage) {

Set the title of the Stage:

        primaryStage.setTitle("JavaFX App");

Instantiate the ToolBar using the new constructor:

        ToolBar toolBar = new ToolBar();

Instantiate a button and add it to the ToolBar:

        Button button1 = new Button("Button 1");
        toolBar.getItems().add(button1);

Also add a seperator as well as a second button:

        toolBar.getItems().add(new Separator());

        Button button2 = new Button("Button 2");
        toolBar.getItems().add(button2);

Add the ToolBar to the VBox:

        VBox vBox = new VBox(toolBar);

Instantiate a Scene and pass in the VBox as well as the dimensions:

        Scene scene = new Scene(vBox, 960, 600);

Set the scene to the stage and show the stage:

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Here is the full code:

package com.jenkov.javafx.toolbar;

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

public class ToolBarExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        ToolBar toolBar = new ToolBar();

        Button button1 = new Button("Button 1");
        toolBar.getItems().add(button1);

        toolBar.getItems().add(new Separator());

        Button button2 = new Button("Button 2");
        toolBar.getItems().add(button2);

        VBox vBox = new VBox(toolBar);

        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Step 4: Run

Copy the above code, add static main method and from it instantiate the above class and run.

Reference

Here are the reference links:

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