Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
ContextMenu component
I have just released my second component for Vaadin 6.2, ContextMenu.
ContextMenu is a popup dialog menu that is inteded to be used with LayoutClickListeners. The purpose of this component is to popup an operating system like ContextMenu, that would apper when you right click on your desktop.
ContextMenu support submenus so it's hierarchical. API resembels Vaadin's MenuBar component, so by adding new item to menu you get reference to menu item as a return value. This reference can be used to insert sub menus.
Online demonstration is currently available from my virtuallypreinstalled site at http://peter.virtuallypreinstalled.com/ContextMenu
Sources are also available and are placed to http://dev.vaadin.com/browser/incubator/ContextMenu/
Great component!
Suggestion: how about automatically opening a submenu after a short hover period over the parent item?
Henri's idea sounds very good indeed. I implemented the requested feature in version 2.1 and it's now available. I also added checks to make sure that the opened menu will fit to screen area. If it won't, it will be opened leftwards from clicked location. This also works with submenus.
New is also updated to virtuallypreinstalled, so you can test it from there if you wish.
Hi,
I'm using your component as ...
ContextMenu cm= new ContextMenu();
ContextMenu.ContextMenuItem color= cm.addItem("Color");
ContextMenu.ContextMenuItem white=color.addItem("White");
ContextMenu.ContextMenuItem red=color.addItem("Red");
ContextMenu.ContextMenuItem blue=color.addItem("Blue");
cm.addListener(new ContextMenu.ClickListener() {
@Override
public void contextItemClick(
org.vaadin.peter.contextmenu.ContextMenu.ClickEvent event) {
// TODO Auto-generated method stub
System.out.println(event.getClickedItem().getName());
//mytextfield.setColor(event.getClickedItem().getName());
}
});
but when i 've clicked on item white, red or blue then the output not shows "White","Red" or "Blue"
What am i doing wrong??
Thank you!!
Hi I'm a beginner with Vaadin and I'm trying to use this component but I always get this error:
Widgetset does not contain implementation for org.vaadin.peter.contextmenu.ContextMenu. Check its @ClientWidget mapping, widgetsets GWT module descrioption file and re-compile your widgetset. Unrendered UIDL: org.vaadin.peter.contextmenu.ContextMenu(NO CLIENT IMPLEMENTATION FOUND)
What should I do? Thanks.
Peter K: What should I do? Thanks.
It seems you haven't recompiled your widgetset after you've added the ContextMenu add-on to your project.
I'm having the same problem. Any help for this?
Xavier Lopez Lopez: Hi,
I'm using your component as ...ContextMenu cm= new ContextMenu(); ContextMenu.ContextMenuItem color= cm.addItem("Color"); ContextMenu.ContextMenuItem white=color.addItem("White"); ContextMenu.ContextMenuItem red=color.addItem("Red"); ContextMenu.ContextMenuItem blue=color.addItem("Blue"); cm.addListener(new ContextMenu.ClickListener() { @Override public void contextItemClick( org.vaadin.peter.contextmenu.ContextMenu.ClickEvent event) { // TODO Auto-generated method stub System.out.println(event.getClickedItem().getName()); //mytextfield.setColor(event.getClickedItem().getName()); } });
but when i 've clicked on item white, red or blue then the output not shows "White","Red" or "Blue"
What am i doing wrong??
Thank you!!
Hi and sorry that it's taken so long to answer. I've been quite busy.
I looked through the code and was almost certain that there's nothing wrong with it until I realized that there's an elementary bug in ContextMenu. Click is recognized only when opening a sub menu.
Sorry about it, I'll fix it right away and release a new version.
I've now released a new 2.2 version which is available from the Directory. Hopefully this one works with out any problems, sorry for the inconvenience.
Jouni Koivuviita:
It seems you haven't recompiled your widgetset after you've added the ContextMenu add-on to your project.
Thanks, got it working finally.
But I have an other problem. How to set a proper position in the layoutClick method for the menu to appear.
The current:
menu.show(event.getClientX(), event.getClientY());
Works only if you don't scroll the window, if you do, the position get's messed up.
Hi! I can't get the hide()-function work for this component. Contextmenu disappears only if I click something, but I can't find any other ways to remove it. It doesn't help if I try to for example remove it from the layout.
Jouni Koivuviita:
It seems you haven't recompiled your widgetset after you've added the ContextMenu add-on to your project.
Hi Jouni!
May be it's posible in Eclipse, but i can't find solution in Netbeans. What do you mean "recompile your widgetset"? Clean & Build in Netbeans?
Hi Heli,
Could you find any workarounf for disapearing the context menu? I have called the hide() function of the context menu every time when got a trigger in the contextItemClick(ClickEvent event) & it seems to work...
BR,
;-D
contextMenu.addListener(new ContextMenu.ClickListener() {
public void contextItemClick(ClickEvent event) {
mainWindow.showNotification(event.getClickedItem().getName());
// do actions
contextMenu.hide();
}
It would be great to have such features in the Table/Tree Action handler (I mean hierarchical, disabled or invisble entries).
Romain Wilbert: It would be great to have such features in the Table/Tree Action handler (I mean hierarchical, disabled or invisble entries).
This has been requested by others as well - see the feature request in UserVoice.
There is a related Vaadin enhancement request: ticket #4343.
Hi Peter,
What you've done is very useful.
I've got one issue with it. I have combined it with Drag&Drop Layout addon.
Each time I rightclick and don't choose any of context menu (leftclick elsewhere)
and then drag and drop some layout element, the contextmenu appears without right-click,
like it is just hidden and not closed.
Thanx for any idea
Hi!
I have the same problem with the widget-set.
I try to use the context menu in a tabsheet and I have re-compiled the widget-set very often.
Widgetset does not contain implementation for org.vaadin.peter.contextmenu.ContextMenu. Check its @ClientWidget mapping, widgetsets GWT module descrioption file and re-compile your widgetset. Unrendered UIDL:
org.vaadin.peter.contextmenu.ContextMenu(NO CLIENT IMPLEMENTATION FOUND)
What can I do?
Thomas Boeck: Hi!
I have the same problem with the widget-set.
I try to use the context menu in a tabsheet and I have re-compiled the widget-set very often.
Widgetset does not contain implementation for org.vaadin.peter.contextmenu.ContextMenu. Check its @ClientWidget mapping, widgetsets GWT module descrioption file and re-compile your widgetset. Unrendered UIDL:
org.vaadin.peter.contextmenu.ContextMenu(NO CLIENT IMPLEMENTATION FOUND)What can I do?
Do you see the folder in WebContent/VAADIN/widgetsets for you custom widgetset and does your web.xml contain a reference to that compiled widgetset?
Yes, I have checked that.
The Folder exists: WebContent ---> VAADIN --> widgetsets ---> to.my.package.widgetset.prototypeWidgetset
with the generated data
web.xml
<servlet>
<servlet-name>Prototype</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<description>
Vaadin application class to start</description>
<param-name>application</param-name>
<param-value>to.my.package.Prototype</param-value>
</init-param>
<init-param>
<description>
Application widgetset</description>
<param-name>widgetset</param-name>
<param-value>to.my.package.widgetset.prototypeWidgetset</param-value>
</init-param>
</servlet>
the generated widgetset.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
<module>
<inherits name="com.vaadin.terminal.gwt.DefaultWidgetSet" />
<inherits name="org.vaadin.peter.contextmenu.ContextmenuWidgetset" />
</module>
By the way, I am using Liferay...do I have to do some configuration in an other xml?
Ok the contextmenu doesn't run in Liferay. Liferay needs other coordinates.
Thank you, Peter. Your component is very usefull. Are you going to support the HierarchicalContainer as a data source for your component? Sometime it is usefull to have ability to switch beetween different contexts. Here is small example how I see it:
HierarhicalContainer firstTypeContext = new HierarhicalContainer();
HierarhicalContainer secondTypeContext = new HierarhicalContainer();
...
if( object instanseof FirstType )
contextMenu.setContainerDataSource(firstTypeContext);
if( object instanseof SecondType )
contextMenu.setContainerDataSource(secondTypeContext);
It would be great if we were able to add a ClickListener on each ContextItemMenu.
I think that the author no longer supports this component
Andris Lapinsh: I think that the author no longer supports this component
The author is busy with other projects at the moment - maybe he gets back to this later, but I suppose patches here might be useful to others if somebody implements some of these changes.
Context menu is not working in current vaadin version (6.5.4) i compile and it generates these exceptions:
[ERROR] Errors in 'jar:file:/C:/Users/1234/workspace/netx/WebContent/WEB-INF/lib/vaadin-6.5.4.jar!/com/vaadin/terminal/gwt/client/ui/VDragAndDropWrapper.java'
[ERROR] Line 175: The method scheduleDeferred(Scheduler.ScheduledCommand) in the type Scheduler is not applicable for the arguments (new Command(){})
[ERROR] Errors in 'jar:file:/C:/Users/1234/workspace/netx/WebContent/WEB-INF/lib/vaadin-6.5.4.jar!/com/vaadin/terminal/gwt/client/ui/VScrollTable.java'
[ERROR] Line 833: The method scheduleDeferred(Scheduler.ScheduledCommand) in the type Scheduler is not applicable for the arguments (new Command(){})
[ERROR] Line 1531: The method scheduleDeferred(Scheduler.ScheduledCommand) in the type Scheduler is not applicable for the arguments (new Command(){})
[ERROR] Line 1566: The method scheduleDeferred(Scheduler.ScheduledCommand) in the type Scheduler is not applicable for the arguments (new Command(){})
It seems that your are using old GWT jars. If you are using Eclipse plugin to compile the widgetset, try to change your project's Vaadin version to an older Vaadin version and then back in project's properties. This operation should update the version of GWT to correct.
Hey everyone! New version of ContextMenu (3.0) has been released!
I apologize for not being able to provide support or maintenance for the component for almost past nine months. New 3.0 version introduces few new features and also fixes few open bugs:
- ContextMenu can now be opened next to any component using new show(Component) method
- Support for icons in menu items has been added
- Easier theming for menu items, supports addStyleName(String) methods
- Improved client - server communication
- Bugfixes for menu not closing properly or reappearing when reloading page
There are still many features that this component does not yet support. Please keep on sending ideas and bug reports, from now on I'll try to release more frequent updates if needed.
At the moment there is a small limitation opening ContextMenu to "component location" using show(Component) method. Given component must have debugId specified using the setDebugId(String) method. This is because debug id is unique identifier in HTML-dom and client-side implementation uses it to figure out component's location. This could be easily automated but it appears that there is related Vaadin bug:
http://dev.vaadin.com/ticket/6748
Cheers!
Hi,
Seems like a great component, but I can't get to try it.
I tried :
menu.show(event.getClientX(), event.getClientY());
on an item of my tree, and I tried :
menu.show(button);
.. but both of those tests display nothing on my screen.
Did I do something wrong ?
Hi, maybe you forgot to add context menu to parent vaadin component before using show method
parent.addComponent(menu)
Thank you Boris, that worked !
EDIT : that worked, but strangely the menu appears about 200px up from the click position. Should I bypass this problem by adding those 200px to the Y parameter of my menu.show(X, Y), or is there a better solution ?
EDIT : In fact I noticed that the menu appears above the click position when I scroll down the page. (I am using it inside a Liferay portlet)
I have the same experience with this scrolling (in Liferay).
Something doesn't realize that user scrolls down.
Tomas
Ugo, you can look at sorce VContextMenu.java, I saw there magic number 200. Maybe it helps you understand
I noticed that when you use ContextMenu for a button, it works well wether you scroll down or not.
I debugged my application and it seems like the event.getClientY() returns a different position when you scroll the page. Maybe that come from the listener, I am having a look at it.
For those who still try to find out how to get a context menu for a tree, the simplest solution I found is to add an action handler to your tree.
cf. http://demo.vaadin.com/docs/example-source/com/vaadin/demo/featurebrowser/TreeExample.java.html
Can ContextMenu be used on a Button or Link or just those components that can support child components?
I have a "choice of download options" I'd like to present when someone clicks on a simple Button, though in my case the Button is in a Table. This way, I could avoid having several buttons next to each other.
Or would I be better off without a Button with a ContextMenu attached to it and use something else, like a simple MenuBar? I like the look of a Button more, and a Link may be okay though less obvious to notice.