How can I call a method every n seconds?
I want to do a slideshow with Swing and CardLayout and every n seconds
it must show a different image calling a different method
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
then in your main you can schedule the task:
first number is initial delay, second is the time between calls to
run()of yourTimerTask: 5000 is 5 seconds.As BalusC noted usually you dispatch swing changes on AWT event thread. In this simple cause it shouldn’t create problems when changing background from an outside thread, in any case you should use
to dispatch your change on the right thread.
If you prefer BalusC approach just use an
ActionListener:They both provide same functionality, but this later one is already prepared to work out together with Swing threads mantaining compatibility and avoiding strange synchronizations issues.