Context Menu added from Java

Reply
Posts: 85
Joined: Nov 24, 2008
Jun 22, 2009

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 ::

Fragments:

    ...
        // 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.setLabel("Download");
		iDownload.setImage("/inc/download.png");
		iDownload.addEventListener("onClick", new EventListener() {
			public void onEvent(Event event) throws Exception {
				Filedownload.save(file.get_path(), null);
			}
		});
		iDownload.setParent(popup);
		// 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
-James

 
Posts: 85
Joined: Nov 24, 2008
Jun 23, 2009

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

 
Posts: 11
Joined: Feb 26, 2008
Jun 26, 2009

James,

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) {
        super(parent);

        this.setId(java.util.UUID.randomUUID().toString());
        this.setTitle("View");
        this.setClosable(true);
        try {
            this.setMode("overlapped");
        } catch (InterruptedException ex) {
        }

        _controller = new Controller(this);
    }

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

        Groupbox gpBox = new Groupbox();
        gpBox.setClosable(false);
        gpBox.appendChild(new Caption("Location"));
        gpBox.setParent(this);

        _viewListBoxContextMenu.setParent(gpBox);
        _changeLocationMenuItem.setId("ChangeLocationContextPopup");
        _changeLocationMenuItem.setParent(_viewListBoxContextMenu);

        _viewListBox.setId("LocationListBox");
        _viewListBox.setWidth("100%");
        _viewListBox.setHeight("450px");
        _viewListBox.setMultiple(false);
        _viewListBox.setParent(gpBox);

        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"));
        lh.setParent(_viewListBox);

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

        return;
    }

    @Override
    public void onClose() {
        super.onClose();
        return;
    }
}

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;}

    @Override
    public void render(Listitem item, Object data) {

        item.setContext(_contextPopup);
        item.setValue( (Contact)data);
        item.setVisible(true);

        //Do Other Work

       return;
    }
}


Hope this helps a little.
William

 
Posts: 49
Joined: Oct 17, 2008
Jun 26, 2009

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.

 
Posts: 85
Joined: Nov 24, 2008
Jun 30, 2009

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,
-James

 
Posts: 7
Joined: Jul 09, 2009
Nov 05, 2009

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

 
Posts: 5
Joined: Oct 15, 2009
Nov 29, 2009

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?

Thanks.

Regards,
Peter

 
Reply

Zorum Version 2.0 Copyright © 2005-2010 Potix Corporation All rights reserved.