ToolBar class is a part of JavaFX. A ToolBar is a control which displays items vertically or horizontally. Buttons, ToggleButtons, and Separators are generally placed into ToolBar. You can also insert any Node into them. Overflow button will appear if there are too many items to fit in the ToolBar which allow selecting items that are not currently visible in the toolbar. ToolBar sets focusTraversable to false.
Constructor of the class:
- ToolBar(): Creates an empty toolbar.
- ToolBar(Node… n): Creates a tool bar populated with the specified nodes.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getItems() | Returns the items of the toolbar. |
| getOrientation() | Returns the orientation of the toolbar. |
| setOrientation(Orientation v) | Sets the value of the orientation of the object. |
Below program illustrate the use of ToolBar class:
- Java program to create a toolbar and add it to the scene: In this program we will create a Toolbar named toolbar. We will also create a Label named label and two Buttons named button1 and button2 and add them to the toolbar. Add the toolbar to the VBox named vbox and add the VBox to the scene. Then add the scene to the stage and call the show() function to display the results.
// Java program to create a toolbar// and add it to the sceneimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;ÂÂpublicclassToolbarextendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("creating toolbar");           Â// create a label           ÂLabel label =newLabel("Toolbar");           Â// creating buttons           ÂButton button1 =newButton("Button1");           ÂButton button2 =newButton("Button2");           Â// creating toolbar           ÂToolBar toolbar =newToolBar(label, button1, button2);           Â// create a VBox           ÂVBox vbox =newVBox(toolbar);           Â// create a scene           ÂScene scene =newScene(vbox,300,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java program to create a toolbar and add it to the scene and set the orientation of the toolbar: In this program we will create a Toolbar named toolbar. We will also create a Label named label and two Buttons named button1 and button2 and add them to the toolbar by using the getItems().add() function. Set the orientation of the toolbar using the setOrientation() function. Now add the toolbar to the HBox named hbox and add the HBox to the scene. Finally add the scene to the stage and call the show() function to display the results.
// Java program to create a toolbar and// add it to the scene and set orientation// of the toolbarimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;importjavafx.geometry.*;ÂÂpublicclassToolbar_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("creating toolbar");           Â// create a label           ÂLabel label =newLabel("Toolbar");           Â// creating buttons           ÂButton button1 =newButton("Button1");           ÂButton button2 =newButton("Button2");           Â// creating toolbar           ÂToolBar toolbar =newToolBar();           Â// add items           Âtoolbar.getItems().add(label);           Âtoolbar.getItems().add(button1);           Âtoolbar.getItems().add(button2);           Â// set orientation of the toolbar           Âtoolbar.setOrientation(Orientation.VERTICAL);           Â// create a HBox           ÂHBox hbox =newHBox(toolbar);           Â// create a scene           ÂScene scene =newScene(hbox,300,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ToolBar.html

