Context Menu added from Java

asked 2009-06-23 00:53:01 +0800

james gravatar image james
255 2

In a grid RowRenderer.java file, I want to dynamically add a context menu to each row.
I found this thread ... http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D6784%3BcategoryId%3D14%3B
terrytornado mentions something about what I need.
:: quote ::


        // you must search for the correct 'onRightClick' writing 
   item.addEventListener("onClick", new myItemClickEventListener());

   /*inner Class
   public class myItemClickEventListener() implements org.zkoss.zk.ui.event.EventListener {
     public void onEvent(Event event) throws UiException {
        try {
          doSomething // here you can write the logic for your popup
        catch (Exception e) {

:: end quote ::

I tried doing it without making another class though, because I'd like to use just the java.lang.Object data i get in the RowRenderer.
So I tried this...

fileLabel.addEventListener("onRightClick", new EventListener() {
	public void onEvent(Event event) throws Exception {
		Menupopup popup = new Menupopup();
		Menuitem iDownload = new Menuitem("iDownload");
		iDownload.addEventListener("onClick", new EventListener() {
			public void onEvent(Event event) throws Exception {
				Filedownload.save(file.get_path(), null);
		// for testing purposes right now, I'm only trying this one

I've tried not adding "row.setContext(popup);" inside the addEventListener, adding it outside the event listener, and even not adding it at all... nothing is happening. Originally I tried just making the Menupopup without the 'addEventListener("onRightClick"...' section.
All of my attempts have only disabled the right click. No context menu appears; mine, nor the normal one in the web browser.

If anyone can help me figure out why the context menu isn't working right, I'd appreciate it.
Thank you

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2009-06-23 17:25:01 +0800

james gravatar image james
255 2

Does anyone know how to add a context menu to a grid row dynamically through a RowRenderer (dynamcailly created grid)?

link publish delete flag offensive edit

answered 2009-06-26 16:46:13 +0800

klotzw gravatar image klotzw


I create a context menu dynamically for application with as follows. I am generating the context menu
for a Listbox, but approach would be the same except renderer code would change to install the context menu
on a row instead of a listitem.

public class View extends AppWindow {
    private static final long serialVersionUID = -5982827231487794662L;

    // Form Fields
    final private Listbox           _viewListBox     = new Listbox();
    final private Menupopup         _viewListBoxContextMenu     = new Menupopup();
    final private Menuitem	    _changeLocationMenuItem = new Menuitem("Change Location");

    public View(Component parent) {

        try {
        } catch (InterruptedException ex) {

        _controller = new Controller(this);

    public void initialize(Component comp, Map<String,Object> params) {
        // Call Base Class Initialization.
        super.initialize(comp, params);

        Groupbox gpBox = new Groupbox();
        gpBox.appendChild(new Caption("Location"));



        Listhead lh = new Listhead();
        lh.appendChild(new Listheader("Name",null,"200px"));
        lh.appendChild(new Listheader("Location",null,"300px"));
        lh.appendChild(new Listheader("Office Number",null,"100px"));
        lh.appendChild(new Listheader("Mobile Number",null,"100px"));
        lh.appendChild(new Listheader("Pager Number",null,"100px"));

        ViewItemRenderer itemRenderer = ViewItemRenderer.getInstance();
        _viewListBox.setModel( _contactSharedData.getProxy(this.getDesktop()));


    public void onClose() {

public class ViewItemRenderer implements ListitemRenderer {
    private final static ViewItemRenderer _instance = new ViewItemRenderer();
    private Menupopup           _contextPopup = null;

    private ViewItemRenderer() {

    public static ViewItemRenderer getInstance() {
        return _instance;

    public void setContextMenu(Menupopup contextPopup) {this._contextPopup = contextPopup;}
    public Menupopup getContextMenu() {return this._contextPopup;}

    public void render(Listitem item, Object data) {

        item.setValue( (Contact)data);

        //Do Other Work


Hope this helps a little.

link publish delete flag offensive edit

answered 2009-06-26 23:24:36 +0800

jyluo gravatar image jyluo
147 1 1

Hi, James, according to my experience, adding context menu dynamically in ZK is indeed tricky, not because it does not work, but due to some weird default settings. 1. First you need to set a page to you context menu. Make sure you invoke Menupopup.setPage() after you create your menu. You can use the page returned by getPage() method from any existing components.
2. Second you need to set an explicit width of the context menu. It does not resize automatically. So the menu will be 0px wide if you don't do this, making it appearing nothing happened although the menu has actually been created and displayed.

link publish delete flag offensive edit

answered 2009-06-30 22:37:30 +0800

james gravatar image james
255 2

Thank you both for your help. Sorry for the delay in my own reply.
jyluo, your suggestions worked perfectly.
I add Menupopup.setPage(row.getPage()) and Menupopup.setWidth(width) and it comes up just like it should.

Thank you,

link publish delete flag offensive edit

answered 2009-11-05 10:20:05 +0800

ravi526 gravatar image ravi526

jyluo---Thanks for your input. I wasted so many days without having idea of setting page for menu popup.

link publish delete flag offensive edit

answered 2009-11-29 17:53:18 +0800

pbabka gravatar image pbabka

updated 2009-11-29 23:58:29 +0800

Hi guys,

This is working perfectly.
My problem is: I can't figure out that how can I get an ID (inside of the eventListener) of the row of the grid on which I clicked when the context menu appeared?



link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2009-06-23 00:53:01 +0800

Seen: 2,412 times

Last updated: Nov 29 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More