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

  • Home
  • SEARCH
  • 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 6244815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:23:37+00:00 2026-05-24T12:23:37+00:00

I would like to implement a countdown timer in Wicket. I have a clock

  • 0

I would like to implement a countdown timer in Wicket. I have a clock class:

public class Clock extends Label{
    private static int time;
    public Clock(int mytime,String id,String message){
    super(id,message);
        time=mytime;
        this.setDefaultModelObject(message);
    }

    public int getTimeLeft(){
        return time;
    }

    public void decrement(){
        time--;
    }
}

and here I attempt to update it every second:

clock.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(1)){
    protected void onTimer(AjaxRequestTarget target){
        clock.decrement();
        target.addComponent(clock);
    }           
});

This doesn’t work because onTimer is a final method and therefore cannot be overridden. What is the correct approach?

  • 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-05-24T12:23:38+00:00Added an answer on May 24, 2026 at 12:23 pm

    There are two problems here.

    1. Where does time appear in the generated label markup? The model simply contains the message so even if time is changed, it won’t make any difference.
    2. As you mentioned, onTimer() is final, and even if it wasn’t, there’s no guarantee that it would be invoked precisely every second.

    So the solution, as so often in Wicket is to turn it upside down, instead of pushing data into your output, let the framework pull it in. This is what you need to do.

    1. You don’t need a separate Clock class, use just a plain Label with nothing overridden.
    2. Create an IModel<String> subclass that stores the timestamp when the clock should be at zero.
    3. Make the getObject() method of the model return a string that contains the difference between the clock’s zero time and the current time.
    4. Set an instance of this as the model of your Label and add the ajax timer behaviour too.
    5. Done.

    Your model will be something like this:

    public class ClockModel implements IModel<String> {
        private long zeroTime;
    
        public ClockModel( int timeInSecs ) {
            this.zeroTime = System.currentTimeMillis() + (timeInSecs * 1000 );
        }
    
        public String getObject() {
            return String.valueOf( ( zeroTime - System.currentTimeMillis() ) / 1000 );
        }
    
    }
    

    You can probably guess the rest.

    Update: just one more thing, kind of obvious but may be worth mentioning: in a real world application make sure you check that time left isn’t negative.

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

Sidebar

Related Questions

I'm trying to implement a countdown timer into a pre-existing public class and I
I would like to implement a simple substitution cipher to mask private ids in
I would like to implement an current property in my class which returns the
I would like to implement a drawing class with the help of Quartz .
I would like to implement a TreeWritable class to represent a Tree structure. I
I would like to implement the observer pattern similar to Timer component. Instead of
I would like to implement the following: import org.springframework.web.servlet.support.RequestContextUtils as RCU class HomeController {
I would like to know how should I implement a count down timer in
I would like implement a button to show another view. I have defined the
I would like to implement something similar to a c# delegate method in PHP.

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.