JavaFX | ToolBar Class

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




