Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:57:28+00:00 2026-06-13T18:57:28+00:00

I’m trying to create a link that will hide or show a part of

  • 0

I’m trying to create a link that will hide or show a part of my page. The link should be reusable and display one of two images, depending on state.

Adding the two subcomponents on every page where I use the link is kind of clunky so I wanted to create a component that behaves like a link while automatically adding its content.

This is the Link component:

public class ToggleVisibilityLink extends AjaxFallbackLink<Boolean>
{
  public ToggleVisibilityLink(final String id, final IModel<Boolean> model)
  {
    super(id, model);

    setOutputMarkupId(true);

    add(new Image("collapseImage")
    {
      @Override
      public boolean isVisible()
      {
        return !getModelObject();
      }
    });
    add(new Image("expandImage")
    {
      @Override
      public boolean isVisible()
      {
        return getModelObject();
      }
    });
  }

  @Override
  public void onClick(final AjaxRequestTarget target)
  {
    setModelObject(!getModelObject());
    if (target != null)
    {
      target.add(this);
      send(this.getParent(), Broadcast.EXACT, target);
    }
  }
}

And this is how I currently use it in HTML (this is added to the page or panel where I use the link):

<a href="#" wicket:id="collapseExpandLink" class="collapseExpandLink">
  <wicket:link>
    <img src="collapse.png" wicket:id="collapseImage" class="collapseExpandImage collapse">
  </wicket:link>
  <wicket:link>
    <img src="expand.png" wicket:id="expandImage" class="collapseExpandImage expand">
  </wicket:link>
</a>

And the corresponding Java call:

add(new ToggleVisibilityLink("collapseExpandLink", new PropertyModel(this, "hidden")));

But I want to be able to skip the body inside the link as one would have to know about the internals of ToggleVisibilityLink.
I experimented with IMarkupResourceStreamProvider, using Dynamic markup in Wicket as a starting point. By googling I found another example where the poster was only able to get that to work when using a Panel, and I was able to do that as well. But I’d really like to keep the link and not package it inside a Panel, as I would not be able to style the link in the markup.

I’m also open to alternatives to encapsulate the link and its body.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:57:29+00:00Added an answer on June 13, 2026 at 6:57 pm

    I was able to get this to work using setBody(), even though I was trying to sabotage myself quite badly (I had duplicate libraries, my own incompatible jQuery library import and a custom resource versioning strategy).

    Here is the current ToggleVisibilityLink:

    public class ToggleVisibilityLink extends AjaxFallbackLink<Boolean>
    {
      static {
        Application.get().getSharedResources().add("ToggleVisibilityLinkCollapse",
                                                   new MyPackageResource(ToggleVisibilityLink.class, "collapse.png"));
        Application.get().getSharedResources().add("ToggleVisibilityLinkExpand",
                                                   new MyPackageResource(ToggleVisibilityLink.class, "expand.png"));
      }
    
      public ToggleVisibilityLink(final String id, final IModel<Boolean> model)
      {
        super(id, model);
    
        setOutputMarkupId(true);
        setEscapeModelStrings(false);
    
        setBody(new BodyModel(model));
      }
    
      @Override
      public void onClick(final AjaxRequestTarget target)
      {
        setModelObject(!getModelObject());
        if (target != null)
        {
          target.add(this);
          send(this.getParent(), Broadcast.EXACT, target);
        }
      }
    
      private static final class BodyModel extends AbstractReadOnlyModel<String>
      {
        private final IModel<Boolean> model;
    
        private BodyModel(final IModel<Boolean> model)
        {
          this.model = model;
        }
    
        @Override
        public String getObject()
        {
          return this.model.getObject() ?
                  "<img src=\""
                + RequestCycle.get().urlFor(new SharedResourceReference("ToggleVisibilityLinkExpand"), null)
                + "\" class=\"collapseExpandImage expand\">"
                  :
                  "<img src=\""
                + RequestCycle.get().urlFor(new SharedResourceReference("ToggleVisibilityLinkCollapse"), null)
                + "\" class=\"collapseExpandImage collapse\">";
        }
      }
    }
    

    Where MyPackageResource is a simple Implementation of PackageResource (why is that constructor protected?).

    Then one can simply add the ToggleVisibilityLink to a container:

    super.add(new ToggleVisibilityLink("collapseExpandLink", new PropertyModel(this, "hidden")));
    

    and

    <a wicket:id="collapseExpandLink" class="collapseExpandLink"></a>
    

    and get notified via Event when the link is clicked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.