-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi
I have a div component with a context menu:
<div context="popupMenu">....
The context menu is:
<menupopup id="popupMenu">
<menuitem label="Edit" id="editMenu" />
.......
</menupopup>
I wrote a EventListener in Java code:
editMenu.addEventListener("onClick", new EventListener(){
public void onEvent(Event event) throws Exception {
System.out.println("editMenu.onClick: " + event);
}}
);
The context menu appears and the event listener is called correctly. Now the question is, how can I retieve the component where the popup menu was opened and the menuitem was selected? In my case I want to have the div component where the popup menu was opened.
The event object in the onEvent callback does not seem to have the div component. The event object has a parent object (getParent()), but this is the menuitem object from the popup.
Thanks
Dieter
Thank you very much for your help. The onOpen() event of the popup is the solution.
There is an easy way to remember the component which opened the popup:
// add an onOpen event listener to the popup which remembers the referencedComponent in a 'hidden' attribute 'ref' of the popup menu popupMenu.addEventListener("onOpen", new EventListener(){ public void onEvent(Event event) throws Exception { OpenEvent openEvt = (OpenEvent)event; Popup popup = (Popup)openEvt.getTarget(); Component referencedComponent = openEvt.getReference(); // set the referenced object in a hidden reference of the popup popup.setAttribute("ref", referencedComponent); }} ); // now we can read that reference again in the 'onClick' event of the menuitem via the attribute 'ref' of the popup menu editMenu.addEventListener("onClick", new EventListener(){ public void onEvent(Event event) throws Exception { Component menuitem = event.getTarget(); Component popup = menuitem.getParent(); Component referencedComponent = (Component)popup.getAttribute("ref"); // continue with business logic on referencedComponent }} );
This approach has the advantage that its independent from the underlying Composer class.
- Dieter
I cannot cast the event to a ForwardEvent. I get a MouseEvent in the onEvent callback.
I got folllowing exception:
>>java.lang.ClassCastException: org.zkoss.zk.ui.event.MouseEvent cannot be cast to org.zkoss.zk.ui.event.ForwardEvent
>> at zk.cc.CompositeDesigner$4.onEvent(CompositeDesigner.java:126)
>> at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:196)
...
Thanks
Dieter
If you will get the target component who is called the event. You must cast the Event to the right Event class.
Not tested :
public void onEvent(Event event) throws InterruptedException { // first, casting to the right event String id = ((ForwardEvent) event).getOrigin().getTarget().getId(); System.out.println(id); if (id.equals("menuItem_1_id")) { // do something here } else if (id.equals("menuItem_2_id")) { // do something there } }
best
Stephan
Asked: 2010-08-11 03:31:49 +0800
Seen: 1,018 times
Last updated: Oct 25 '10