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:

  1. ToolBar(): Creates an empty toolbar.
  2. 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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a toolbar
    // and add it to the scene
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
      
    public class Toolbar extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("creating toolbar");
      
                // create a label
                Label label = new Label("Toolbar");
      
                // creating buttons
                Button button1 = new Button("Button1");
                Button button2 = new Button("Button2");
      
                // creating toolbar
                ToolBar toolbar = new ToolBar(label, button1, button2);
      
                // create a VBox
                VBox vbox = new VBox(toolbar);
      
                // create a scene
                Scene scene = new Scene(vbox, 300, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:

  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a toolbar and
    // add it to the scene and set orientation
    // of the toolbar
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.geometry.*;
      
    public class Toolbar_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("creating toolbar");
      
                // create a label
                Label label = new Label("Toolbar");
      
                // creating buttons
                Button button1 = new Button("Button1");
                Button button2 = new Button("Button2");
      
                // creating toolbar
                ToolBar toolbar = new ToolBar();
      
                // 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 = new HBox(toolbar);
      
                // create a scene
                Scene scene = new Scene(hbox, 300, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.