Creating a custom context menu
Vaadin supports a simple context menu (mouse right-click) only for table, tree, and calendar. The menu items are handled as actions by an action handler. To enable a context menu, we have to implement a Vaadin Action.Handler
and add it to the component with the addActionHandler()
method. However, if we need a complex context menu on a different component, we can use the ContextMenu
add-on created by Peter Lehto. In this recipe, we will create a context menu for the text area. We will add the context menu with three actions. One action cleans the text area and the two others insert a date and a name in the text area.
Getting ready
We create a Vaadin project with a main UI class called
Demo
.public class Demo extends UI {…}
We will use the
ContextMenu
add-on. We download it from the Vaadin directory (http://vaadin.com/addon/contextmenu) and put the JAR file to our web project under theWebContent/WEB-INF/lib
directory.Alternatively, we can add Maven dependency according...