- 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 context menu to an image in JavaFX?
A context menu is a popup menu that appears on interacting with the UI elements in the application. You can create a context menu by instantiating the javafx.scene.control.ContextMenu class. Just like a menu, after creating a context menu, you need to add MenuItems to it.
Typically, a context menu appears when you “right-click” on the attached control.
Setting ContextMenu to nodes −
You can set ContextMenu to any object of the javafx.scene.control class, using the setContextMenu() method.
Every node has a property named onContextMenuRequested, this defines a function to be called when a context menu has been requested on this Node. You can set value to this property using the setOnContextMenuRequested() menu.
To set a context menu to an image view, create an ImageView object embedding the desired image, invoke the setOnContextMenuRequested() method on it.
Example
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.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.Clipboard; import javafx.scene.input.ClipboardContent; import javafx.scene.input.ContextMenuEvent; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ContextMenuImage extends Application { @Override public void start(Stage stage) throws FileNotFoundException { //Creating the image view ImageView imageView = new ImageView(); //Setting the image view parameters imageView.setFitWidth(575); // imageView.setFitHeight(295); imageView.setPreserveRatio(true); InputStream stream = new FileInputStream("D:\images\elephant.jpg"); Image image = new Image(stream); imageView.setImage(image); //Creating a context menu ContextMenu contextMenu = new ContextMenu(); //Creating the menu Items for the context menu MenuItem item1 = new MenuItem("Copy"); MenuItem item2 = new MenuItem("remove"); contextMenu.getItems().addAll(item1, item2); //Setting action to the context menu item item1.setOnAction((ActionEvent e) -> { Clipboard clipboard = Clipboard.getSystemClipboard(); ClipboardContent content = new ClipboardContent(); content.putImage(imageView.getImage()); clipboard.setContent(content); }); //Setting action to the context menu item item2.setOnAction((ActionEvent e) -> { imageView.setVisible(false); }); //Setting context menu to the image view imageView.setOnContextMenuRequested(new EventHandler() { @Override public void handle(ContextMenuEvent event) { contextMenu.show(imageView, event.getScreenX(), event.getScreenY()); } }); HBox box = new HBox(); box.setPadding(new Insets(10, 10, 10, 10)); box.getChildren().add(imageView); //Setting the stage Group root = new Group(box); Scene scene = new Scene(root, 595, 360, Color.BEIGE); stage.setTitle("CustomMenuItem"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to add image to the menu item in JavaFX?
- How to add mnemonics to a menu in JavaFX?
- How to add scroll bar to an image in JavaFX?
- How to add a separator to a Menu in JavaFX?
- How to add an image to a button (action) in JavaFX?
- How to add an image as label using JavaFX?
- How to add the slider to a menu item in JavaFX?
- How to add custom button to the right click/context menu in Excel?
- How to add image patterns to nodes in JavaFX?
- How to add a navigation menu on an image with CSS?
- How to create a context menu for an element in HTML5?
- How to create context menus in JavaFX?
- How to display an image in JavaFX?
- How to toggle menu items in JavaFX?
- How to disable a menu item in JavaFX
