Shikhar13 / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

HTML screen appends content on consecutive setPage() calls. #168

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
protected void startApp() throws MIDletStateChangeException {       
        Display.init(this);                 
        Display.getInstance().callSerially(new Runnable() { 
            public void run() {         
                browser = new WebBrowser();              
                Object browserInternal = browser.getInternal();
                if (browserInternal instanceof HTMLComponent) {          
                    HTMLComponent htmlComponent = (HTMLComponent) browserInternal;
                    htmlComponent.setIgnoreCSS(false);
                    htmlComponent.setEventsEnabled(true);
                    htmlComponent.setRequestHandler(new MyDefaultDocumentRequestHandler());         
                }

                Form f = new Form();    
                f.setLayout(new GridLayout(1, 1));
                f.addComponent(browser);
                f.show();

                browser.setPage("<html><head></head><body>Go fish!</body></html>", null);
                browser.setPage("<html><head></head><body>Oh man!</body></html>", null);
            }});
    }

What you see on the screen is this:
Go fish!
Oh man!

I would expect a set() call to replace not append.

Original issue reported on code.google.com by jkoo...@gmail.com on 19 Apr 2012 at 12:12

GoogleCodeExporter commented 9 years ago
Btw, this is j2me

Original comment by jkoo...@gmail.com on 19 Apr 2012 at 12:13

GoogleCodeExporter commented 9 years ago
You need to follow events in the HTMLComponent to see that loading completed 
and only then start on something new. 
Yes the code should block against that misuse but it doesn't and fixing it will 
probably break allot more.

Original comment by shai.almog on 19 Apr 2012 at 12:31

GoogleCodeExporter commented 9 years ago
Wow nice tip man thanks! :)

Here's the solution:

protected void startApp() throws MIDletStateChangeException {       
        Display.init(this);                 
        Display.getInstance().callSeriallyAndWait(new Runnable() { 
            public void run() {         
                browser = new WebBrowser();              
                Object browserInternal = browser.getInternal();
                if (browserInternal instanceof HTMLComponent) {          
                    HTMLComponent htmlComponent = (HTMLComponent) browserInternal;
                    htmlComponent.setIgnoreCSS(false);
                    htmlComponent.setEventsEnabled(true);
                    htmlComponent.setRequestHandler(new MyDefaultDocumentRequestHandler());         
                }

                Form f = new Form();    
                f.setLayout(new GridLayout(1, 1));
                f.addComponent(browser);
                f.show();

                browser.setPage("<html><head></head><body>Go fish!</body></html>", null);                           
            }});

        Object browserInternal = browser.getInternal();
        if (browserInternal instanceof HTMLComponent) {          
            HTMLComponent htmlComponent = (HTMLComponent) browserInternal;
            while (htmlComponent.getPageStatus() != HTMLCallback.STATUS_COMPLETED) {
                System.out.println("" + htmlComponent.getPageStatus());
                Bozza.sleep(20);
            }
        }

        Display.getInstance().callSeriallyAndWait(new Runnable() { 
            public void run() {         
                browser.setPage("<html><head></head><body>Oh man!</body></html>", null);
            }
        });
    }

Original comment by jkoo...@gmail.com on 19 Apr 2012 at 12:50

GoogleCodeExporter commented 9 years ago
even better,

if (htmlComponent.getPageStatus() >= HTMLCallback.STATUS_REQUESTED) {
                while (htmlComponent.getPageStatus() != HTMLCallback.STATUS_COMPLETED) {
                    System.out.println("" + htmlComponent.getPageStatus());
                    Bozza.sleep(20);
                }   
            }       

Original comment by jkoo...@gmail.com on 19 Apr 2012 at 12:57

GoogleCodeExporter commented 9 years ago
Closing since this is inherent in HTMLComponent and has a working process.

Original comment by shai.almog on 22 Nov 2013 at 9:12