views:

147

answers:

2

I have created some custom composite widget, which listens to an events (in my case loginEvent). Once the event is caught the state of the widget changes so as the way it should look (in my case I would like to change one of the icons to a signal that the user is logged in).

How, after the event is cought, can I make the widget draw itself again according to the new state?

I am pretty new to the gwt so be gentle and please elaborate...

+1  A: 

There is many ways to do this. This example shows how to show Login/Logout and toggle visibility when a user logged in event is raised.

public class LoginLogoutWidget extends Compostite {
    private final Anchor m_loginLink;
    private final Anchor m_logoutLink;

    public LoginLogoutWidget() {

     m_loginLink = new Anchor("Login");
     //Register event handlers etc

     m_logoutLink = new Anchor("Logout");
     //Register event handlers etc

     HorizontalPanel hp = new HorizontalPanel();

     hp.add(m_loginLink);
     hp.add(m_logoutLink);

     initWidget(hp);

     //Default login visible, and logout invisible
     m_logoutLink.setVisible(false);
    }

    public void onLoginEvent(boolean loggedIn) {  
     m_loginLink.setVisible(!loggedIn);
     m_logoutLink.setVisible(loggedIn); 
    }
}
Fedearne
A: 

If you have something like this:

HorizontalPanel panel = new HorizontalPanel();
Image oldImage = new Image("url to old img");
panel.add(oldImage);

Then you should be able to do something like this:

panel.remove(oldImage);
Image newImage = new Image("url to new img");
panel.add(newImage);

panel.layout();

I've found that in some cases, if I don't call the "layout()" method on the parent container, then things that I've added programmatically don't show up on the screen like I expect them to.

Hope that helps!

Dave Paroulek
panel.layout() - That sounds like GXT, not core GWT to me.... No, things do not work as you'd expect with GXT since for a variety of reasons, changes are not instantly applied to the DOM, unlike normal GWT. This leads to all sorts of excess calls to .layout() and similar methods. At least it did in our code before we began to move away from that library. Sidetrack over, otherwise good points.
bikesandcode