0

I have a JFrame which contains a JPanel with the JButton "Press Me"

enter image description here

Pressing the "Press me" button will change to another JPanel(SecondPanel) within the same JFrame

enter image description here

I am facing a problem where there is a 10 second delay when i press the "Press Me" button before the SecondPanel appears.

This 10 second delay is caused the Timer event .

I wish for the SecondPanel to appear before the Timer event start.

What is happening now is that the Timer event starts , i am waiting at the "Press Me" button for 10 seconds , before the SecondPanel appears ,

can someone help me resolve this issue

Thanks

Main class used to run the project

package testing;

import java.io.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;


public class Testing extends JPanel 
{



    public static void main(String[] args) 
    {
        frame = new JFrame();
        LoginPanel lp = new LoginPanel();
        frame.add(lp);
        frame.pack();
        frame.validate();
        frame.setVisible(true);

    }

   static JFrame frame;
}

LoginPanel class

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginPanel extends JPanel
{
    LoginPanel()
    {
        Loginbtn = new JButton("Press Me");
        Loginbtn.addActionListener(new LoginButtonListener());
        add(Loginbtn);


    }

     private class LoginButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {

            SecondPanel sp = new SecondPanel();
            Utility.ChangePanel(sp);
            sp.run();
        }
    }

    JButton Loginbtn;

}

SecondPanel class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class SecondPanel extends JPanel
{
    SecondPanel()
    {
        setLayout(new GridLayout(2,2));

        //set deck image
        File deckfile = new File("./src/testing/Ace_Club_1_1.png"); //deck image file location

        try
        {    
          Deckimg = ImageIO.read(deckfile); //read deck image


        }

        catch (IOException e)
        {

        }

         Image scaledInstance = Deckimg.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
         DeckLabel = new JLabel(new ImageIcon(scaledInstance));
         add(DeckLabel);

    }

    public void run()
    {
        Timer timer = new Timer(5000, new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                // Code to be executed
                System.out.println("HowareYou");

            }
        });
        timer.setRepeats(false); // Only execute once
        timer.start(); // Go go go!

        try
        {
             Thread.sleep(7000);
        }



        catch(InterruptedException ie)
        {

        }

    }

    JLabel DeckLabel;
    JPanel DeckPanel;
    BufferedImage Deckimg;

}

Utility class used to switch JPanels within the JFrame

package testing;

import java.security.MessageDigest;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Utility 
{    
    public static void ChangePanel(JPanel jp)
    {


        testing.Testing.frame.getContentPane().removeAll();
        testing.Testing.frame.add(jp);
        testing.Testing.frame.validate();


    }

}
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • there are two mistakes 1st. timer is initialized from run() and 2nd. killed by Thread.Sleep – mKorbel Apr 26 '14 at 08:13
  • utility is useless class use CardLayout, better is to use JLabel.setIcon(my local varibale initialized on apps startup) – mKorbel Apr 26 '14 at 08:15
  • scalled instalne is pretty asynchonous, carrefully with, have to calculating with possible delays (no icon is painted, only mouse hover_over repainting proper image) – mKorbel Apr 26 '14 at 08:17
  • @mKorbel, what's wrong with creating the `Timer` in the `run` method? Also, calling `Thread.sleep` won't 'kill' the timer, it will still run on another thread, or is that not what you meant? I agree that using `CardLayout` would be better, but that's not the cause of @Computernerd's issue. – SimonC Apr 26 '14 at 08:18
  • @SimonC wrong idea withpossible side effects (everything inside run() can kill ....) – mKorbel Apr 26 '14 at 08:19
  • @mKorbel, sorry, but that doesn't make any sense. Are you trying to say that anything running on the event thread can block the GUI? If so then, that's still not a reason not to start a timer there. Granted, as it stands, the `Timer` instance could be created on another thread, and only started on the event thread, but I don't the difference would be noticeable. – SimonC Apr 26 '14 at 08:21
  • (but that doesn't make any sense == read Oracle tutorial swing - How to use timers)see usage of [Swing Timer](http://stackoverflow.com/a/7049095/714968) – mKorbel Apr 26 '14 at 08:28
  • Sorry, but that's a really bad answer. – SimonC Apr 26 '14 at 08:33
  • @SimonC The `run` method is adding confusion into the code, which could be consider a method part of a `Runnable` interface. A different name would help remove the confusion. Many Swing programmers see `run` and panic – MadProgrammer Apr 26 '14 at 09:16
  • What is the purpose of `Thread.sleep(7000)`, why would you want this delay here? What is it's purpose? – MadProgrammer Apr 26 '14 at 09:16
  • @MadProgrammer, yes, that makes sense. I was questioning the objection to starting a `Timer` from a method called on the event thread. – SimonC Apr 26 '14 at 09:48

1 Answers1

3

You're blocking the Swing event thread in SecondPanel.run() with Thread.sleep(7000). This will stop any GUI updates happening. If you remove the sleep you should see the second panel appear before the timer fires.

SimonC
  • 6,590
  • 1
  • 23
  • 40