- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add a separator to a Menu in JavaFX?
A menu is a list of options or commands presented to the user, typically menus contain items that perform some action. The contents of a menu are known as menu items.
To add a separator to a menu, JavaFX provides a special class named javafx.scene.control.Separator. Using this class, you can create a MenuItem that embeds a horizontal separator within it. This comes handy whenever you need to separate menu items.
Once you create a SeperatorMenuItem you can add its object to the menu using the add() or, addAll() method, along with the other menu items.
Example
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.scene.control.SeparatorMenuItem; import javafx.scene.paint.Color; import javafx.stage.Stage; public class SeperatorsToMenu extends Application { public void start(Stage stage) { //Creating a menu Menu fileMenu = new Menu("File"); //Creating menu Items fileMenu.setMnemonicParsing(true); MenuItem item1 = new MenuItem("Add Files"); MenuItem item2 = new MenuItem("Remove File"); MenuItem item3 = new MenuItem("Start Converting"); MenuItem item4 = new MenuItem("Stop Converting"); MenuItem item5 = new MenuItem("Exit"); //Adding all the menu items to the menu fileMenu.getItems().addAll(item1, item2, item3, item4, item5); //Creating separator menu items SeparatorMenuItem sep = new SeparatorMenuItem(); //Adding separator objects to menu fileMenu.getItems().add(4, sep); //Creating a menu bar and adding menu to it. MenuBar menuBar = new MenuBar(fileMenu); menuBar.setTranslateX(200); menuBar.setTranslateY(20); //Setting the stage Group root = new Group(menuBar); Scene scene = new Scene(root, 595, 200, Color.BEIGE); stage.setTitle("Menu Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to add a separator in Menu item in Tkinter?
- How to add mnemonics to a menu in JavaFX?
- How to add a separator for a choice box in JavaFX?
- How to add the slider to a menu item in JavaFX?
- How to create a separator using JavaFX?
- How to add image to the menu item in JavaFX?
- How to add context menu to an image in JavaFX?
- How to disable a menu item in JavaFX
- How to create a Menu using JavaFX?
- How to add separator in a ToolBar with Java?
- How to toggle menu items in JavaFX?
- How to style the separator using CSS in JavaFX?
- How to add accelerators to a menu item?
- How to add data to a TableView in JavaFX?
- How to add custom fonts to a text in JavaFX?

Advertisements