BrowserFrame - how to show loading?

Hello,
I have a BrowserFrame, thats contents is made dynamically:

[code]
private Resource createStreamResource(RegistryDownloader downloader, String ico) {
return new StreamResource(new StreamSource(){

            private static final long serialVersionUID = 6504694687675949426L;

            @Override
            public InputStream getStream() {
                String data;
                try {
                    data = downloader.downloadHtml(ico);
                } catch (IOException e) {
                    log.warn(e);
                    data = "Can't obtain data from registry: " + e.getMessage();
                } catch (EntityNotFoundException e) {
                    log.warn(e);
                    data = "Entitity not found in requsted regisrty: " + e.getMessage();
                }
                try {
                    return new ByteArrayInputStream(data.getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    // can't happen
                    return new ByteArrayInputStream(data.getBytes());
                }
            }
            
        }, downloader.country().get2LCode() + "/" + ico + "/registry_data.html");
    }

[/code]Sometimes it takes a while to create a stream. In such case BrowserFrame show just white space and user can have feeling, that something goes wrong or is missing.
Is there a way to show loading indicator/progress bar/spin until the page is prepared and can be displayed?

This was trickier than I first thought, but I got one possible solution. Add a progressbar into the layout before the browserframe and pass a listener to the streamcreation. Listener should be called just before the created stream is returned.

ProgressBar bar = new ProgressBar();
bar.setIndeterminate(true);
bar.setCaption("Loading content...");

LoadingListener listener = new LoadingListener() {
    @Override
    public void onFinish() {
        bar.setVisible(false);
    }
};

browser = new BrowserFrame("Browser",createStreamResource(listener));
browser.setImmediate(true);
browser.setWidth("600px");
browser.setHeight("400px");

layout.addComponent(bar);
layout.addComponent(browser);

//...

public interface LoadingListener {
        void onFinish();
}

Hi Johannes,
thanks for unswer, but unfortunatly ProgressBar doesn’t disappear.
onFinish()
method is called, but layout doesn’t change. I’ve tried to add
layout.markAsDirty()
or surround the body of
onFinish()
method with
UI.getCurrent().access
, but nothing helps. :frowning:

Do you have Push enabled either with @Push annotation in your UI or as an init-param in your web.xml?

https://vaadin.com/book/-/page/advanced.push.html

No I don’t use
Push
feature as it causes appication to freeze sometimes.

Thats OK. You can also set your UI to polling before the component is created and disable the polling when it is no more needed.

setPollInterval(200);

//...

setPollInterval(-1);

Great!!! Thank you. Now I even don’t need a progress bar as it is shown for whole application, when waiting for data.