- 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
JavaFX example to set action to the "exit" MenuItem
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.
You can create a menu item by instantiating the javafx.scene.control.MenuItem class.
Setting action to a ContextMenu
The Menu class inherits a property named onAction from the javafx.scene.control.ButtonBase class, which is of the type ObjectProperty<EventHandler<ActionEvent>>. This property represents the action that is invoked whenever you press the button. You can set the value to this property using the setOnAction() method.
To set action on a menu you need to −
Instantiate the Menu class.
Create a MenuItem objects and add them to the Menu.
Invoke the setOnAction() method the menu item object.
To the setOnAction() method you need to pass an EventHandler<ActionEvent> object wrapping the code for the action to perform within it. For exit operation, since you need to close the application you can use System.exit(0).
Example
import javafx.application.Application; import javafx.event.ActionEvent; 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.image.ImageView; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ExitMenuItemAction extends Application { @Override public void start(Stage stage) { //Creating image view files ImageView imgView1 = new ImageView("UIControls/open.png"); imgView1.setFitWidth(20); imgView1.setFitHeight(20); ImageView imgView2 = new ImageView("UIControls/Save.png"); imgView2.setFitWidth(20); imgView2.setFitHeight(20); ImageView imgView3 = new ImageView("UIControls/Exit.png"); imgView3.setFitWidth(20); imgView3.setFitHeight(20); //Creating menu Menu fileMenu = new Menu("File"); //Creating menu Items MenuItem item1 = new MenuItem("Open File", imgView1); MenuItem item2 = new MenuItem("Save file", imgView2); MenuItem item3 = new MenuItem("Exit", imgView3); //Adding all the menu items to the menu fileMenu.getItems().addAll(item1, item2, item3); //Creating a menu bar and adding menu to it. MenuBar menuBar = new MenuBar(fileMenu); menuBar.setTranslateX(200); menuBar.setTranslateY(20); //Setting action to exit menu item item3.setOnAction((ActionEvent t) -> { System.exit(0); }); //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
- JavaFX example to set action (behavior) to the menu item
- JavaFX example to set action listeners to a CheckBox
- Example to set action listeners (behavior) to a ChoiceBox in JavaFX
- How to set action to a RadioButton in JavaFX?
- How to set action to a slider using JavaFX?
- JavaFX example to set slider to the progress bar
- How to embed nodes in a JavaFX MenuItem?
- JavaFX example to set behavior to a button
- How to set JCheckBoxMenuItem for a MenuItem in Java?
- How to add action listeners to ContextMenu in JavaFX?
- How to add an image to a button (action) in JavaFX?
- Set positive action to the Bootstrap 4 card
- Set negative action to the Bootstrap 4 card
- JavaFX example to add a tooltip to a radio button
- JavaFX example to decrease the brightness of an image using OpenCV.
