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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating toolbar");// create a labelLabel label =newLabel("Toolbar");// creating buttonsButton button1 =newButton("Button1");Button button2 =newButton("Button2");// creating toolbarToolBar toolbar =newToolBar(label, button1, button2);// create a VBoxVBox vbox =newVBox(toolbar);// create a sceneScene scene =newScene(vbox,300,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating toolbar");// create a labelLabel label =newLabel("Toolbar");// creating buttonsButton button1 =newButton("Button1");Button button2 =newButton("Button2");// creating toolbarToolBar toolbar =newToolBar();// add itemstoolbar.getItems().add(label);toolbar.getItems().add(button1);toolbar.getItems().add(button2);// set orientation of the toolbartoolbar.setOrientation(Orientation.VERTICAL);// create a HBoxHBox hbox =newHBox(toolbar);// create a sceneScene scene =newScene(hbox,300,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- JavaFX | Pos Class
- JavaFX | Tab Class
- JavaFX | Reflection Class
- JavaFX | ClosePath class
- JavaFX | TitledPane Class
- JavaFX | LineTo class
- JavaFX | BoxBlur Class
- JavaFX | HLineTo Class
- JavaFX | FontWeight Class
- JavaFx | GaussianBlur Class
- JavaFX | SplitPane Class
- JavaFX | Dimension2D Class
- JavaFX | Insets Class
- JavaFX | StackPane Class
- JavaFX | Point2D Class
- JavaFX | Glow Class
- JavaFX | Canvas Class
- JavaFX | Bloom Class
- JavaFX | FontPosture Class
- JavaFX | Stop Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

