JavaFX - ToolBar



A ToolBar is a graphical user interface control with horizontal or vertical strip of buttons, menus, or other controls. It is often used to provide quick access to frequently used commands or functions. We can locate a toolbar at top of any application or web page as shown in the below figure −

Tool Bar

ToolBar in JavaFX

In JavaFX, the tool bar is represented by the ToolBar class. This class is a part of the package named javafx.scene.control. By instantiating this class, we can create a ToolBar node in JavaFX. It has the following constructors −

  • ToolBar() − It is the default constructor used to create a tool bar without any predefined nodes.

  • ToolBar(Node nodes) − It is the parameterized constructor of ToolBar class which creates a new tool bar with the specified nodes.

Steps to create a tool bar in JavaFX

Follow the steps given below to create a tool bar in JavaFX.

Step 1: Create nodes to display within the ToolBar

First, we need to create a list of nodes to display within the ToolBar where, each node represents a distinct command or function. Here, we are going to create buttons, separator and text fields by using the below code blocks −

// Creating buttons and a text field
Button btnNew = new Button("New");
Button btnOpen = new Button("Open");
Button btnSave = new Button("Save");
Button btnExit = new Button("Exit");
TextField txtSearch = new TextField();
txtSearch.setPromptText("Search");
// creating separators
Separator sepOne = new Separator();
Separator sepTwo = new Separator();

Step 2: Instantiate the ToolBar class

As mentioned earlier, a tool bar is created using the class named ToolBar. Therefore, instantiate this class as shown in the following code block −

// creating a toolbar
ToolBar toolsB = new ToolBar();

Step 3: Add the nodes to the ToolBar object

To add all the nodes to the ToolBar, we use the getItems() and addAll() methods. The code given below demonstrates it−

// adding the nodes
toolsB.getItems().addAll(btnNew, btnOpen, btnSave, sepOne, txtSearch, sepTwo, btnExit);

Step 4: Launching Application

Once the tool bar is created and all the nodes are added, follow the given steps below to launch the application properly −

  • Firstly, instantiate the class named BorderPane by passing the ToolBar object as a parameter value to its constructor. However, we can use any of the layout panes such as GridPane or StackPane.

  • Then, instantiate the class named Scene by passing the BorderPane object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor.

  • Then, set the title to the stage using the setTitle() method of the Stage class.

  • Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  • Display the contents of the scene using the method named show().

  • Lastly, the application is launched with the help of the launch() method.

Example

Following is the program that will create a ToolBar using JavaFX. Save this code in a file with the name ToolbarExample.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ToolbarExample extends Application {
   @Override
   public void start(Stage stage) throws Exception {
      // Creating buttons and a text field
      Button btnNew = new Button("New");
      Button btnOpen = new Button("Open");
      Button btnSave = new Button("Save");
      Button btnExit = new Button("Exit");
      TextField txtSearch = new TextField();
      txtSearch.setPromptText("Search");
      // creating separators
      Separator sepOne = new Separator();
      Separator sepTwo = new Separator();
      // creating a toolbar
      ToolBar toolsB = new ToolBar();
      // adding the nodes
      toolsB.getItems().addAll(btnNew, btnOpen, btnSave, sepOne, txtSearch, sepTwo, btnExit);
      // Creating a BorderPane as root 
      BorderPane root = new BorderPane();
      // adding the ToolBar at the top 
      root.setTop(toolsB);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(root, 400, 300);
      stage.setTitle("ToolBar in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ToolbarExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ToolbarExample

Output

On executing, the above program generates a JavaFX window displaying a ToolBar with specified buttons and text fields as shown below.

ToolBar Output

Setting the Orientation of the ToolBar

To change the orientation of the ToolBar, we can use the setOrientation() method and pass either Orientation.HORIZONTAL or Orientation.VERTICAL as the argument.

Example

In the following JavaFX program, we will create a vertical ToolBar. Save this code in a file with the name VerticalToolbar.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.geometry.Orientation;
public class VerticalToolbar extends Application {
   @Override
   public void start(Stage stage) throws Exception {
      // Creating buttons
      Button btnNew = new Button("New");
      Button btnOpen = new Button("Open");
      Button btnSave = new Button("Save");
      Button btnExit = new Button("Exit");
      // creating separators
      Separator sepOne = new Separator();
      Separator sepTwo = new Separator();
      Separator sepThree = new Separator();
      // creating a toolbar
      ToolBar toolsB = new ToolBar();
      // adding the nodes
      toolsB.getItems().addAll(btnNew, sepOne, btnOpen, sepTwo, btnSave, sepThree, btnExit);
      toolsB.setOrientation(Orientation.VERTICAL); 
      // Creating a BorderPane as root 
      BorderPane root = new BorderPane();
      // adding the ToolBar at the top 
      root.setTop(toolsB);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(root, 400, 300);
      stage.setTitle("ToolBar in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls VerticalToolbar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls VerticalToolbar

Output

When we execute the above code, it will generate the following output.

Toolbar Output2
Advertisements