- 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 (behavior) to the menu item
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.
In JavaFX a menu is represented by the javafx.scene.control.Menu class, a menu item is represented by the javafx.scene.control.MenuItem class.
Setting action to MenuItem
The MenuItem 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.
One of the ways to set an action to an item menu is using the OnAction() method.
Example
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; 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.Image; import javafx.scene.image.ImageView; import javafx.scene.paint.Color; import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.stage.FileChooser.ExtensionFilter; public class MenuActionExample extends Application { public void start(Stage stage) { //Creating a menu Menu fileMenu = new Menu("File"); //Creating menu Items MenuItem item1 = new MenuItem("Open Image"); MenuItem item2 = new MenuItem("Clear"); //Adding all the menu items to the menu fileMenu.getItems().addAll(item1, item2); //Creating a File chooser FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Open Image"); fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*")); //Creating the image view ImageView imageView = new ImageView(); //Setting the image view parameters imageView.setX(10); imageView.setY(45); imageView.setFitWidth(575); imageView.setFitHeight(300); imageView.setPreserveRatio(true); //Handling the event of the Open Image item1.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { try { //File file= fileChooser.showOpenDialog(stage); File file = fileChooser.showOpenDialog(stage); InputStream stream = new FileInputStream(file); Image image = new Image(stream); //Setting image to the image view imageView.setImage(image); } catch (FileNotFoundException e) { e.printStackTrace(); } } }); //Handling the event of the exit menu item item2.setOnAction(event -> { imageView.setImage(null); }); //Creating a menu bar and adding menu to it. MenuBar menuBar = new MenuBar(fileMenu); menuBar.setTranslateX(3); menuBar.setTranslateY(3); //Setting the stage Group root = new Group(menuBar, imageView); Scene scene = new Scene(root, 595, 355, Color.BEIGE); stage.setTitle("Menu Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- Example to set action listeners (behavior) to a ChoiceBox in JavaFX
- JavaFX example to set behavior to a button
- JavaFX example to set action to the "exit" MenuItem
- JavaFX example to set action listeners to a CheckBox
- How to add image to the menu item in JavaFX?
- How to disable a menu item in JavaFX
- How to add the slider to a menu item 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
- Select item from sub-menu of a menu using mouse over action in Selenium
- How to add accelerators to a menu item?
- How to create a Menu using JavaFX?
- How to toggle menu items in JavaFX?
- How to add mnemonics to a menu in JavaFX?

Advertisements