-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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) { }}}
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 } });
If anyone can help me figure out why the context menu isn't working right, I'd appreciate it.
Thank you
-James
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; } }
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.
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
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
Asked: 2009-06-23 00:53:01 +0800
Seen: 2,413 times
Last updated: Nov 29 '09