Results 1 to 11 of 11
  1. #1
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default how to give delay?

    hai,
    I am a beginner in java programming.i want to know how i can give a delay(say 10sec) in java.i have written the code for displaying a page(page1).After that i have created the object for new page(page2).But what i want is a delay of 10sec in between these.my code is:
    import javax.swing.*;
    import java.awt.*;
    public class page1 extends JFrame
    {
    Container c;
    JLabel l1,l2,l3;
    Font f;
    public page1()
    {
    c=getContentPane();
    f=new Font("Helvetica",Font.BOLD+Font.ITALIC,20);
    l1=new JLabel("WELCOME CUSTOMER");
    c.add(l1);
    l1.setForeground(Color.BLUE);
    l1.setBounds(100,100,200,100);
    l2=new JLabel();
    c.add(l2);
    //how can i create a delay here?
    page2 b2=new page2();
    b2.setSize(1000,1000);
    b2.setVisible(true);

    }
    public static void main(String args[])
    {
    page1 b1=new page1();
    b1.setSize(1000,1000);
    b1.setVisible(true);
    }



    }
    somebody please help me with some simple examples.Waiting for earliest reply............................................. ...

  2. #2
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default

    i request senior members here to help me.i am waiting here for the reply.seniors pls help me

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

    Default

    If you've tried Thread.sleep(...) you'll know that it won't work. The answer is to use a Swing Timer. Have a look at the Sun tutorials regarding this animal and give it a try. You won't be disappointed.

  4. #4
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default

    @ Fubarable
    sir,
    can you just give asimple example?

  5. #5
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    try adding this into your code.
    hope can help you.
    Java Code:
     import javax.swing.Timer;
     Timer timer = new Timer(1000, new MyTimerActionListener());
    
     timer.start();
          try
          {
            Thread.sleep(10000);//10000millisecond
    
           }
           catch (InterruptedException ie)
                {
            System.out.println(ie.getMessage());
                }
    timer.stop();
    
    class MyTimerActionListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
    
        System.out.println("waiting");
    
      }
    share your source if successful.
    share more,learn more.
    happy java-ing
    Last edited by angelicsign; 12-16-2008 at 07:15 PM.

  6. #6
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default

    @ angelicsign
    no,its not working.It is remaining standstill(wont display)for 10000 millisecond and after that it is showing two pages.

  7. #7
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    i edited the code above,check it out...

  8. #8
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default

    @ angelicsign
    now it is showing this error

    D:\test>javac page1.java
    page1.java:25: cannot find symbol
    symbol : class MyTimerActionListener
    location: class page1
    Timer timer = new Timer(1000, new MyTimerActionListener());

  9. #9
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    If I were you, I'd write yourself a little utility method to start off with that takes the task that you want to delay and the delay. You can base it on Timer. Something like:

    Java Code:
      public void runDelayed(int noSeconds, final Runnable r) {
        final Timer t = new Timer(noSeconds * 1000, null);
        t.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              r.run();
            } finally {
              t.stop();
            }
          }
        });
        t.start();
      }
    Then, when you want to perform a delayed action, you do something like this:

    Java Code:
    runDelayed(10, new Runnable() {
      public void run() {
        turnPage();
      }
    });
    
    private void turnPage() {
      // ... change components etc
    }
    If you use Timer as in this example, you don't need to use Thread.sleep() -- the timer handles the delay for you.

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

    Default

    Quote Originally Posted by angelicsign View Post
    try adding this into your code.
    hope can help you.
    Java Code:
     import javax.swing.Timer;
     Timer timer = new Timer(1000, new MyTimerActionListener());
    
     timer.start();
          try
          {
            Thread.sleep(10000);//10000millisecond
    
           }
           catch (InterruptedException ie)
                {
            System.out.println(ie.getMessage());
                }
    timer.stop();
    
    class MyTimerActionListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
    
        System.out.println("waiting");
    
      }
    You're calling Thread.sleep(...) on the EDT, the Event Dispatch Thread here. Are you absolutely sure that you want to recommend doing this???

  11. #11
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Animation Delay - Thread problem
    By wererabit in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 10:35 PM
  2. Delay on inputs during calculation
    By matt_well in forum New To Java
    Replies: 14
    Last Post: 07-26-2008, 04:17 PM
  3. How to mesure ping delay in JAVA..?
    By sacr83 in forum Networking
    Replies: 4
    Last Post: 06-15-2008, 06:37 AM
  4. Replies: 1
    Last Post: 02-15-2008, 04:07 PM
  5. Replies: 0
    Last Post: 02-05-2008, 10:59 AM

Posting Permissions

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