- 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 action listeners to ContextMenu 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. You can set ContextMenu to any object of the javafx.scene.control class, using the setContextMenu() method.
Typically, these content menus appear when you “right-click” on the attached control.
Adding action listeners to a ContextMenu
The ContextMenu 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 context menu you need to −
Instantiate the ContextMenu class.
Create a MenuItem object 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.
Example
Following JavaFX example contains a button and, a slider that resizes the button. On “right-click” the button shows two options − Prevent Resizing and Allow Resizing.
If you select the Prevent Resizing option, the button is set to default size and you cannot re-size it further. If you select Allow Resizing again, you can re-size it.
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.MenuItem; import javafx.scene.control.Slider; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class ContextMenu_Action extends Application { public void start(Stage stage) { //Creating a button Button button = new Button("Hello"); button.setPrefSize(60, 60); //Creating a slider to resize the button Slider slider = new Slider(40, 200, 60); //Setting its orientation to Horizontal slider.setPrefHeight(180); slider.setOrientation(Orientation.VERTICAL); slider.setShowTickLabels(true); slider.setShowTickMarks(true); slider.setMajorTickUnit(40); slider.setBlockIncrement(20); slider.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){ button.setPrefSize((double)newValue, (double)newValue); } }); //Creating a context menu ContextMenu contextMenu = new ContextMenu(); //Creating the menu Items for the context menu MenuItem item1 = new MenuItem("Prevent Resizing"); MenuItem item2 = new MenuItem("Allow Resizing"); contextMenu.getItems().addAll(item1, item2); //Setting the ContextMenuItem to the button button.setContextMenu(contextMenu); //Setting action to the context menu item item1.setOnAction((ActionEvent e) -> { button.setMinWidth(60); button.setPrefWidth(60); button.setMaxWidth(60); button.setMinHeight(60); button.setMaxHeight(60); button.setPrefHeight(60); }); item2.setOnAction((ActionEvent e) -> { button.setPrefHeight(60); button.setPrefWidth(60); button.setMinWidth(Button.USE_PREF_SIZE); button.setMaxWidth(Button.USE_PREF_SIZE); button.setMinHeight(Button.USE_PREF_SIZE); button.setMaxHeight(Button.USE_PREF_SIZE); }); //Creating the pane BorderPane pane = new BorderPane(); pane.setCenter(button); pane.setLeft(new VBox(new Label("Button Reize"), slider)); pane.setPadding(new Insets(10, 10, 10, 10)); //Preparing the scene Scene scene = new Scene(pane, 595, 250); stage.setTitle("Context Menu"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
To Continue Learning Please Login
Login with Google