zvjerka24
zvjerka24

Reputation: 1792

GWT surrounding elements with href

Is there some clean solution (or clean workaround) to surround more elements with href. eg. I have something like this

VerticalPanel vp = new VerticalPanel();
Label l1 = new Label("This is label 1");
Label l2 = new Label("This is label 2");
vp.add(l1);
vp.add(l2);

I would like to make href from this construction.

Upvotes: 1

Views: 432

Answers (3)

Zied Hamdi
Zied Hamdi

Reputation: 2660

Create a class HrefVerticalPanel where you override the add method to surround the added element with an Anchor object

class HrefVerticalPanel extends VerticalPanel {
  public void add(Widget w) {
    Anchor surrounding = new Anchor();
    surrounding.add(w);
    super.add(surrounding);
    handleAnchorUrl(surrounding);
  } 

  protected void handleAnchorUrl(Anchor toBeClicked) {...}
}

Upvotes: 0

Igor Klimer
Igor Klimer

Reputation: 15321

A clean(er) solution would be, IMHO, using FocusPanel. You can add a ClickHandler to the whole panel - the result is that whatever you click on in that panel will get handled by that one ClickHandler (no need to add the ClickHandler to all the Widgets inside):

VerticalPanel vp = new VerticalPanel();
Label l1 = new Label("This is label 1");
Label l2 = new Label("This is label 2");
vp.add(l1);
vp.add(l2);

FocusPanel focusPanel = new FocusPanel();
focusPanel.add(vp);
ClickHandler clickHandler = new ClickHanler() {
  public void onClick(ClickEvent event)  {
     Window.open(...);
  }
};
focusPanel.addClickHandler(clickHandler);

Please note that FocusPanel is a SimplePanel - meaning it can hold only one Widget, most likely another Panel, like VerticalPanel in this case. FocusPanel implements a lot of other Handlers, so make sure to check them all out - they tend to be very useful in other use cases.

Upvotes: 2

Dmitry Negoda
Dmitry Negoda

Reputation: 3189

way 1. Use HTMLPanel with outer < A > element:

hp = new HTMLPanel("<a href=... id="myid"></a>");
hp.add(vp, "myid");

way 2. Install clickHandlers on all label elements:

ch = new ClickHanler() {
  public void onClick(ClickEvent event)  {
     Window.open(...);
  }
};
l1.addClickHandler(ch);
l2.addClickHandler(ch);

Upvotes: 1

Related Questions