0

I would like to add an AttributeAppender to a Component inside an AjaxEventBehavior using Apache Wicket. A Behavior has a getComponent() method but in the Constructor getComponent() obvioulsy returns null.

Now I pass the component to the Constructor of the AjaxEventBehavior and it's working but is this a good way to achieve my goal..

Here's what I'm doing:

AjaxTooltipBehavior:

public class AjaxTooltipBehavior extends AjaxEventBehavior {
      public AjaxTooltipBehaviour(String event, Component tooltippedComponent) {
           super(event);
           tooltippedComponent.add(new AttributeAppender("data-tooltip","wicketAjaxTooltip"));
      }    

      ...
}

And that's the way I use it:

 ...
 final WebMarkupContainer icon = new WebMarkupContainer("icon"); //a tooltiped icon
 icon2.add(new AjaxTooltipBehaviour("mouseover",icon2)

I asked myself if there isn't a way to add the AttributeAppender to the componet without passing the component to the AjaxTooltipBehavior. Does anyone know if this is possible in wicket or if there are better solutions? FYI: I'm using wicket 1.6.

Thanks in advance for your support! Ronny

rontron
  • 453
  • 2
  • 6
  • 14

3 Answers3

2

Generally you would override Behavior#onBind(Component), but this method is made final in AbstractAjaxBehavior. But it will call onBind() and you use getComponent() there:

@Override
protected void onBind() {
    super.onBind();
    getComponent().add(new AttributeAppender("data-tooltip","wicketAjaxTooltip"));
}
Christoph Leiter
  • 9,147
  • 4
  • 29
  • 37
  • this solution works, but I found an imho better way. I override 'onConfigure(Component component)' which seems to be the right place to work with the behaviours component. But thank you anyway! – rontron Dec 10 '12 at 11:35
0

Because you have extended from AbstractAjaxBehavior (AjaxEventBehavior extends AbstractAjaxBehavior), you should gain access to getComponent(), which will give you the component the behavior is attached to.

Sarhanis
  • 1,577
  • 1
  • 12
  • 19
-1

I override Behavior#onConfigure(Component component) wich is possible the most suitable way to add Behaviors or do some other stuff with the component belonging to the Behavior.

@Override
protected void onConfigure(Component component) {
   super.onConfigure();
   component().add(new AttributeAppender("data-tooltip","wicketAjaxTooltip"));
}
rontron
  • 453
  • 2
  • 6
  • 14
  • You definitely shouldn't add the attributeappender to onConfigure. This will mean that the attribute appender will be added each time the component is re-rendered and you'll have data-tooltip="wicketAjaxTooltip wicketAjaxTooltip" for instance. – Sarhanis Nov 15 '13 at 02:24