detect button press event from webComponent by addWebEventListener

63 views
Skip to first unread message

Akash Kubavat

unread,
Jan 11, 2015, 9:38:09 PM1/11/15
to codenameone...@googlegroups.com
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans
Desktop OS : Win 8
Simulator : All
Device

I am opening one url in BrowserComponent.

In that I want to detect particular button click event.

For that I have written following code:
BrowserComponent c = new BrowserComponent();
findContainerFF(form).addComponent(c);
c.setURL(url);       

        c.addWebEventListener("onClick", new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                Log.p("evt.toString()=" + evt.toString() + "\n"
                    + "evt.getClass()" + evt.getClass() + "\n"
                    + "evt.getDropTarget()" + evt.getDropTarget() + "\n"
                    + "evt.getSource()" + evt.getSource() + "\n"
                );
            }
        });
               

For the 'type' argument I tried with onclick, onClick, OnClick but I am not getting any output in log at all.

Please can anyone suggest how can I grab the click event of button from BrowserComponent?
Or what correct type I need to pass to handle particular button press event from web browser.

Thanks.


Shai Almog

unread,
Jan 12, 2015, 1:36:19 AM1/12/15
to codenameone...@googlegroups.com
That's not a supported web event type.
To handle events in the web side you need to write JavaScript code and use either the JavaScript bridge or shouldNavigate to callback to the Java side.

Akash Kubavat

unread,
Jan 12, 2015, 10:51:18 PM1/12/15
to codenameone...@googlegroups.com
Hi,

I tried by following examples given in this CN1 link https://codenameone.googlecode.com/svn/trunk/CodenameOne/javadoc/com/codename1/javascript/package-summary.html

As per my below code, when I press refresh button, it should call itself and then it should execute this "onStart" event
but I am not getting log output..

        final BrowserComponent c = new BrowserComponent();
        c.setURL(url);
       
        final JavascriptContext ctx = new JavascriptContext(c);
        c.addWebEventListener("onStart", new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JSObject logger = (JSObject)ctx.get("{}");
                Log.p("before LOGGER");
                logger.set("log", new JSFunction(){
                    public void apply(JSObject self, Object[] args) {
                        Log.p("in Apply");
                        String msg = (String)args[0];
                        Log.p("[Javascript Logger] "+msg);
                    }
                });
                ctx.set("window.logger", logger);
                Log.p("before ExeStrAndRet");
                c.executeAndReturnString("logger.log('my apps output');");
                Log.p("after ExeStrAndRet");
            }
        });    
        findContainerFF(f).addComponent(c);

Output log printing sequence, when I run, is as following
[EDT] 0:5:23,132 - before LOGGER
[EDT] 0:5:23,145 - before ExeStrAndRet
[EDT] 0:5:23,148 - after ExeStrAndRet

Please can you say is it correct ?

Thanks.

Shai Almog

unread,
Jan 13, 2015, 2:09:52 AM1/13/15
to codenameone...@googlegroups.com
Hi,
I suggest adding the listener before setting the URL.

Akash Kubavat

unread,
Jan 20, 2015, 8:24:30 PM1/20/15
to codenameone...@googlegroups.com
I got this working
but just for ref to other developers.

Please note that this JavaScript functionality/testing is not working in simulator..
it is working only in devices.

Thanks @Shai for continuous suggestions
Reply all
Reply to author
Forward
0 new messages