Results 1 to 7 of 7
  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default [SOLVED] Swing Timer issue

    Hi, is there any alternative to using Thread.sleep() to wait a certain amount of seconds within a swing timer? The program is not responsive at times using Thread.sleep(). I have heard its a bad idea with a GUI. I have posted code below.

    Help is greatly appreciated.



    javax.swing.Timer timer = new javax.swing.Timer(1000, new MyTimerActionListener());

    timer.start();

    try
    {
    Thread.sleep(8000);
    }
    catch (InterruptedException e)
    {
    }
    timer.stop();



    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class MyTimerActionListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {



    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Not only is it a "bad idea", you shouldn't use thread.sleep at all in a GUI on the EDT code as it will only serve to put your app to sleep. Why not use a timer itself? That's what it's for after all.

    If this doesn't help, then please give more information on just what exactly the program is supposed to be doing. Good luck.

  3. #3
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Ok, so as you can see in the example I posted, all I want to do is wait a number of seconds. So,for example, how can I wait 8 seconds without using thread.sleep()?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

  5. #5
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    I have looked at the tutorial, but it didn't help. I just want the program to do nothing for x number of seconds. How do I do this?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Timer#setInitialDelay, as you could have easily found out by going through the API.

    And just looking at a tutorial is rarely useful, you need to actually do the exercises.

    db

  7. #7
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    Thanks for the replies. I have found a way to do exactly what I want.
    Cheers.

Similar Threads

  1. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  2. Java1.5 Swing Issue
    By naveenbk in forum AWT / Swing
    Replies: 1
    Last Post: 11-23-2008, 08:03 PM
  3. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 04:09 PM
  4. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 06:47 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •