How to create a ToolBar in JavaFX?

JavaFXObject Oriented ProgrammingProgramming

A toolbar is used to display UI elements such as buttons, toggle buttons, and separators, etc.. You cannot add any other nodes to a toolbar. The contents of a toolbar can be arranged either horizontally or vertically. You can create a toolbar by instantiating the javafx.scene.control.ToolBar class.

Example

The following Example demonstrates the creation of a ToolBar.

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
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.scene.paint.Color;
import javafx.stage.Stage;
public class ToolBarExample extends Application {
   public void start(Stage stage) {
      Button bt1 = new Button("Java");
      Button bt2 = new Button("JavaFX");
      Separator sep1 = new Separator();
      Button bt3 = new Button("Neo4J");
      Button bt4 = new Button("MongoDB");
      Separator sep2 = new Separator();
      Button bt5 = new Button("Python");
      Button bt6 = new Button("C++");
      //Creating a tool bar
      ToolBar toolBar = new ToolBar();
      //Retrieving the items list of the toolbar
      ObservableList list = toolBar.getItems();
      list.addAll(bt1, bt2, sep1, bt3, bt4, sep2, bt5, bt6);
      //Creating a vbox to hold the pagination
      VBox vbox = new VBox();
      vbox.setSpacing(15);
      vbox.setPadding(new Insets(50, 50, 50, 100));
      vbox.getChildren().addAll(toolBar);
      //Setting the stage
      Scene scene = new Scene(new Group(vbox), 595, 150, Color.BEIGE);
      stage.setTitle("Tool Bar");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 19-May-2020 10:57:57
Advertisements