
- JavaFX - Environment
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Path Objects
- JavaFX - Path Objects
- JavaFX - LineTo Path Object
- JavaFX - HLineTo Path Object
- JavaFX - VLineTo Path Object
- JavaFX - QuadCurveTo Path Object
- JavaFX - CubicCurveTo Path Object
- JavaFX - ArcTo Path Object
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX - Linear Gradient Pattern
- JavaFX - Radial Gradient Pattern
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX - Color Adjust Effect
- JavaFX - Color input Effect
- JavaFX - Image Input Effect
- JavaFX - Blend Effect
- JavaFX - Bloom Effect
- JavaFX - Glow Effect
- JavaFX - Box Blur Effect
- JavaFX - GaussianBlur Effect
- JavaFX - MotionBlur Effect
- JavaFX - Reflection Effect
- JavaFX - SepiaTone Effect
- JavaFX - Shadow Effect
- JavaFX - DropShadow Effect
- JavaFX - InnerShadow Effect
- JavaFX - Lighting Effect
- JavaFX - Light.Distant Effect
- JavaFX - Light.Spot Effect
- JavaFX - Point.Spot Effect
- JavaFX - DisplacementMap
- JavaFX - PerspectiveTransform
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX - Rotation Transformation
- JavaFX - Scaling Transformation
- JavaFX - Translation Transformation
- JavaFX - Shearing Transformation
- JavaFX Animations
- JavaFX - Animations
- JavaFX - Rotate Transition
- JavaFX - Scale Transition
- JavaFX - Translate Transition
- JavaFX - Fade Transition
- JavaFX - Fill Transition
- JavaFX - Stroke Transition
- JavaFX - Sequential Transition
- JavaFX - Parallel Transition
- JavaFX - Pause Transition
- JavaFX - Path Transition
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Handling Media
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - MenuBar
A menubar is a graphical user interface component that displays a horizontal row of menus, each containing a list of commands or options. It is located at the top of a window or screen, and provides a convenient way for users to access the functionality of an application. A typical menubar looks like the below figure −

Creating MenuBar in JavaFX
In JavaFX, the menubar control is represented by a class named MenuBar. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a MenuBar control in JavaFX.
In addition to the MenuBar class, we also require the following classes −
Menu
The Menu class represents a single menu in the menubar. It has a text property that defines its label, and an items property that holds a list of menu items. To create a Menu, use the code given below −
//Creating a menu Menu objectOfMenu = new Menu("nameOfMenu");
MenuItem
The MenuItem is used to create a single command or option within a menu. It has a text property that defines its label, and an onAction property that defines what happens when the user selects it. It is created by using the below code −
//Creating menu items for the menu MenuItem menuItemObject = new MenuItem("nameOfMenuItem");
Example
The following example illustrates how to create a MenuBar in JavaFX application. Save this code in a file with the name JavafxmenuBar.java.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.stage.Stage; public class JavafxmenuBar extends Application { public void start(Stage stage) { //Creating the first menu Menu category = new Menu("Category"); //Creating menu items for the menu MenuItem itemOne = new MenuItem("Programming"); MenuItem itemTwo = new MenuItem("Cyber Security"); MenuItem itemThree = new MenuItem("Marketing"); //Adding all the items to the category category.getItems().addAll(itemOne, itemTwo, itemThree); //Creating another menu Menu library = new Menu("Library"); //Creating menu items for the library menu MenuItem itemFour = new MenuItem("HTML"); MenuItem itemFive = new MenuItem("Java"); MenuItem itemSix = new MenuItem("JavaFX"); //Adding all the items to library menu library.getItems().addAll(itemFour, itemFive, itemSix); //Creating the third menu Menu articles = new Menu("Articles"); //Creating menu items for the articles MenuItem itemSeven = new MenuItem("Constructor"); MenuItem itemEight = new MenuItem("Inheritance"); MenuItem itemNine = new MenuItem("Current Affairs"); //Adding all items to the menu articles.getItems().addAll(itemSeven, itemEight, itemNine); //Instantiating the MenuBar class MenuBar newmenubar = new MenuBar(); //Adding all the menus to the MenuBar object newmenubar.getMenus().addAll(category, library, articles); //Setting the stage Group newgroup = new Group(newmenubar); Scene scene = new Scene(newgroup, 600, 400); stage.setTitle("MenuBar in JavaFX"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar
Output
When we execute the above code, it will generate the following output.

How to add Icons to the MenuItems?
To add icons to the menuitems, we call the setGraphic() method which accepts an object of ImageView class. The icon will be appear next to the MenuItem name.
Example
In the following JavaFX application, we are going to demonstrate how to add icons for the menuitems within MenuBar. Save this code in a file with the name JavafxmenuBar.java.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.stage.Stage; import java.io.File; import javafx.scene.image.ImageView; public class JavafxmenuBar extends Application { public void start(Stage stage) { //Creating the first menu Menu category = new Menu("Category"); //Creating menu items for the menu MenuItem itemOne = new MenuItem("Programming"); MenuItem itemTwo = new MenuItem("Cyber Security"); MenuItem itemThree = new MenuItem("Marketing"); // adding icons to the menuitems itemOne.setGraphic(new ImageView("file:programming.png")); itemTwo.setGraphic(new ImageView("file:cyber.png")); itemThree.setGraphic(new ImageView("file:marketing.png")); //Adding all the items to the category category.getItems().addAll(itemOne, itemTwo, itemThree); //Creating another menu Menu library = new Menu("Library"); //Creating menu items for the library menu MenuItem itemFour = new MenuItem("HTML"); MenuItem itemFive = new MenuItem("Java"); MenuItem itemSix = new MenuItem("JavaFX"); //Adding all the items to library menu library.getItems().addAll(itemFour, itemFive, itemSix); //Creating the third menu Menu articles = new Menu("Articles"); //Creating menu items for the articles MenuItem itemSeven = new MenuItem("Constructor"); MenuItem itemEight = new MenuItem("Inheritance"); MenuItem itemNine = new MenuItem("Current Affairs"); //Adding all items to the menu articles.getItems().addAll(itemSeven, itemEight, itemNine); //Instantiating the MenuBar class MenuBar newmenubar = new MenuBar(); //Adding all the menus to the MenuBar object newmenubar.getMenus().addAll(category, library, articles); //Setting the stage Group newgroup = new Group(newmenubar); Scene scene = new Scene(newgroup, 600, 400); stage.setTitle("MenuBar in JavaFX"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Compile and execute the saved Java file from the command prompt by using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar
Output
On executing the above JavaFX code, it will generate the following output.
