andriy
andriy

Reputation: 4154

Wicket: Is it possible to update label value from the thread?

For example I have a html span which should be updated when the application is doing some tests (like do post, then wait some time for the response and represent result). So I want to show on this span current progress of this action.

I've tried create thread in myPage.java which extends WebPage. It's definition is:

class TestThread extends Thread{

    private Label current_message_lbl;
    private Component component;

    public void run() {
        try {
            this.sleep(1000);
            current_message_lbl.setDefaultModelObject(new StringResourceModel("test-SENDING-COMMANDS",component, null));
                    /* HTTP POST and other test code*/

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void setCurrentMesssageLabel(Label current_message_lbl){
        this.current_message_lbl = current_message_lbl;
    }
    public void setComponent(Component component){
        this.component = component;
    }
}

And I start it with:

TestThread thread = new TestThread();
thread.setCurrentMesssageLabel(current_message_lbl);
thread.setComponent(component);
thread.start();

And after I'm getting this error message

There is no application attached to current thread Thread-103

I've tried also define thread like:

class TestThread extends WicketApplication implements Runnable{

Upvotes: 1

Views: 926

Answers (1)

bhdrkn
bhdrkn

Reputation: 6692

You can add AjaxSelftUpdatingTimeBehavior to your component. Than this Behavior updates the component in every seconds or else. But you must change component's model in your Thread.

Also if you try to lazy load a component you can check AjaxLazyLoadPanel. There are some examples in here and here.

Upvotes: 4

Related Questions