1. the user clicks the button(Go to animation page) at page one
2. the user will be directed to page two whereby an animation is played for 5 seconds
3. after 5 seconds, the user will be directed to page three.

I am trying to delay a page for around 5seconds before it loads another page.

I used thread.sleep(5000) in my Jbutton and check the current thread state. But this is wrong as
as soon as the current thread state is TIMED_WAITING, it will go straight to third page without playing the animation

I don't really know how to make use of threading to tell the program such that after the animation
has played for 5 seconds, it will automatically direct the user to third page.

Please advice and help me. Thanks

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PageOne {
    private JPanel panelCont = new JPanel();
    private JButton btn1 = new JButton("btn 1");
    private JPanel panelFirst = new JPanel();
    private JPanel panelSecond = new JPanel();
    private JPanel panelThird = new JPanel();
    private CardLayout c1 = new CardLayout();
    private JFrame frame = new JFrame();

    public PageOne() {
        panelSecond.add(new PageTwo());
        panelThird.add(new PageThree());

        panelCont.add(panelThird,"3");, "1");

        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Thread t1 = new Thread(new Runnable() { 
                    public void run() { 
                        try {
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                if(t1.getState() == Thread.State.RUNNABLE) {
          , "2");

                if(t1.getState() == Thread.State.TIMED_WAITING) {
          , "3");



    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PageOne();

import java.awt.BorderLayout;    
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PageTwo extends JPanel {

    public PageTwo() {
         setLayout(new BorderLayout());
         Icon icon = new ImageIcon("animation.gif");
         JLabel label = new JLabel(icon);

import javax.swing.JLabel;
import javax.swing.JPanel;

public class PageThree extends JPanel  {
    public PageThree() {
        JLabel label = new JLabel("This is Page 3");

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your actionPerformed method starts NOTHING else will happen in Swing, including no screen updates, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.
If you want to do stuff in steps over a period of time, here is the right way:
In your actionPerformed start a javax.swing.Timer (as mKorbel said), and return. When the timer fires you can update whatever needs updating and return. Inbetween the timer firings Swing will be free to update the screen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.