8

I have a java frame that I want to close it automatically after 3 or 4 seconds. I found out I must used threads. but I dont know how exactly to do it, this a dumy part of my code :

package intro;

import java.awt.*;
import java.io.IOException;
//import view.LangMenu;

public class IntroClass extends Frame {

    private int _screenWidth = 0;
    private int _screenHeight = 0;
    private int _screenCenterx = 0;
    private int _screenCentery = 0;

    //private static final String SOUND_PATH="/sounds/introSound.midi";
    public IntroClass() {
        Toolkit thisScreen = Toolkit.getDefaultToolkit();
        Dimension thisScrrensize = thisScreen.getScreenSize();

        _screenWidth = thisScrrensize.width;
        _screenHeight = thisScrrensize.height;
        _screenCenterx = _screenWidth / 2;
        _screenCentery = _screenHeight / 2;
        setBackground(Color.pink);
        Label lbl = new Label("Welcome To Dots Game. Samaneh Khaleghi", Label.CENTER);
        add(lbl);
        setUndecorated(true);
        setLocation((_screenCenterx*50)/100,_screenCentery-(_screenCentery*50)/100);
        setSize((_screenWidth * 50) / 100, (_screenHeight * 50) / 100);

        WaitClass r = new WaitClass();
        r.start();
        view.DotsBoardFrame d=new view.DotsBoardFrame();
                main.Main.showScreen(d);
    }

    class WaitClass extends Thread {

        boolean running = true;

        public void run() {
            while (running) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }                
            }
        }
    }
}

3 Answers 3

6

Although AWT is supposed to be thread-safe, it isn't really. So I suggest, like Swing, do all the GUI manipulation on the AWT Event Dispatch Thread (EDT).

For this particular task, javax.swing.Timer should do the trick. (Although it is in the javax.swing package, there is nothing Swing-specific about it.)

Also I would strongly suggest not extending classes unless you really have to. There is very little reason ever to extend Thread or Frame (unfortunately there are lots of bad examples and old tutorials out there).

0
3

in your frame start a new thread and pass to it your frame instance, and after a specific period of time close it.

class MyThread extends Thread {

  private JFrame frame;
  //-- getters and setters for frame

  public void run() {
    Thread.sleep(1000); //close the frame after 1 second.
    frame.close();
  }

}

and in your JFrame class, in the constructor specifically put the following line of code:

MyThread th = new MyThread();
th.setFrame(this);
th.start();
3
  • Thanks Omar, I am gonna try it :)
    – user261002
    Feb 16, 2010 at 22:07
  • 4
    Bad idea to manipulate an AWT component from any thread except the EDT. Feb 16, 2010 at 22:25
  • 1
    @user261002: If its working means give the credit to the man Omar. You asked 9 questions,but you didn't accept any ans.
    – Venkat
    Mar 16, 2010 at 11:38
2

You can use a Timer and let it take care of threads for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.