- 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 embed nodes in a JavaFX MenuItem?
A menu is a list of options or commands presented to the user. In JavaFX a menu is represented by the javafx.scene.control.Menu class, you can create a menu by instantiating this class.
A menu item is an option in the menu it is represented by the javafx.scene.control.MenuItem class, a superclass of the Menu class. You can display a text or a graphic as a menu item and add the desired cation to it.
Setting a node as a menu item
The MenuItem class has a property named graphic this is of the type Node; this specifies the optional graphic for the current menu-item. You can set the value to this property using the setGraphic() method.
To embed a node as a menu item you need to create an object of it by instantiating the respective class and pass it as a parameter to the setGraphic() Method.
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.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.CullFace; import javafx.scene.shape.DrawMode; import javafx.scene.shape.Sphere; import javafx.stage.Stage; public class NodeAsMenuItem extends Application { @Override public void start(Stage stage) { //Drawing a Sphere Sphere sphere = new Sphere(); sphere.setRadius(12.0); sphere.setDrawMode(DrawMode.LINE); //Setting other properties sphere.setCullFace(CullFace.BACK); sphere.setDrawMode(DrawMode.FILL); PhongMaterial material = new PhongMaterial(); material.setDiffuseColor(Color.BROWN); sphere.setMaterial(material); //Creating menu Menu fileMenu = new Menu("File"); //Creating menu item MenuItem item = new MenuItem("Open"); //Setting slider as a menu item item.setGraphic(sphere); //Adding all the menu items to the menu fileMenu.getItems().addAll(item); //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"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- JavaFX example to set action to the "exit" MenuItem
- How to add colors to nodes in JavaFX?
- How to add image patterns to nodes in JavaFX?
- How to disable a MenuItem in Java?
- How to change the orientation of nodes in a tile pane using JavaFX?
- How to set JCheckBoxMenuItem for a MenuItem in Java?
- How to embed JavaScript in HTML file?
- How to embed Lua codes in Java?
- How to embed base64 images in HTML?
- How to embed a video using HTML code?
- How to embed fonts in PDFs produced by Matplotlib
- How to embed an interactive Matplotlib plot on a webpage?
- HTML DOM MenuItem Object
- How to embed youtube as an audio player?
- How to create a Button in JavaFX?
