This discussion is archived
10 Replies Latest reply: Jan 27, 2013 12:42 AM by 831755 RSS

How to add time event to show jframe component one after another sequetial?

831755 Newbie
Currently Being Moderated
I want to create the visual expression of merge sort to explain it.

So what I want is first in textbox initial sequence of number. And the subsequently next state with some time delay sequentially.

Can any one tell me how to do.
I tried by creating Thread like:

PrimeThread p = new PrimeThread();
p.start();
jTextField1.setVisible(false);
jTextField2.setVisible(false);
p.sleep(1000);
jTextField1.setVisible(true);
p.sleep(2000);
jTextField2.setVisible(true);

but this does not work. All of them appearing at same moment! I want them to appear with some delay one after another..
  • 1. Re: How to add time event to show jframe component one after another sequetial?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

    db
  • 2. Re: How to add time event to show jframe component one after another sequetial?
    831755 Newbie
    Currently Being Moderated
    I want to show how merge sort perform visually using jframe. What I want to do is to make visible subsequent jlable with some time delay. I tried many way but all of them appears at same moment with no intermediate delay.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    // jLabel1.setVisible(false);
    jLabel2.setVisible(false);
    jLabel3.setVisible(false);
    jLabel4.setVisible(false);
    jLabel5.setVisible(false);
    jLabel6.setVisible(false);
    jLabel7.setVisible(false);
    final Timer t=new Timer((4000), null);
    final int delay=2000;
    final ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {

    jLabel1.setVisible(true);
    t.getDelay();

    jLabel2.setVisible(true);
    t.setDelay(3000);

    jLabel3.setVisible(true);
    t.setDelay(2000);

    jLabel4.setVisible(true);
    t.setDelay(2000);

    jLabel5.setVisible(true);
    t.setDelay(2000);

    jLabel6.setVisible(true);
    t.setDelay(2000);
    }
    };

    new Timer(delay, taskPerformer).start();


    But when I click button all the lables appear at same momenet though I have kept delay.
  • 3. Re: How to add time event to show jframe component one after another sequetial?
    EJP Guru
    Currently Being Moderated
    What did you learn from reading the link that Darryl provided?
  • 4. Re: How to add time event to show jframe component one after another sequetial?
    831755 Newbie
    Currently Being Moderated
    previously I created Thread and tried to put delay by using thread.sleep() event!

    Then as per the link given by Derryl I used swing timer, then tried using timer.getDelay(), timer.setDelay() kind of method.
    As dont get the use of animator.repaint() method!
  • 5. Re: How to add time event to show jframe component one after another sequetial?
    jduprez Pro
    Currently Being Moderated
    You didn't get the point of the swing Timer or of the listener.
    The listener's actionPerformed method is invoked (and completely executes) each time the timer fires, including the first time it fires.
    So the first time the timer fires, it executes all of this:
    public void actionPerformed(ActionEvent evt) {
    
    jLabel1.setVisible(true); // shows label1
    t.getDelay(); // reads a value but does nothing with it
    
    jLabel2.setVisible(true); // shows label 2
    t.setDelay(3000); // set the delay for the next time the timer fires, but does not stop execution of the current method
    
    jLabel3.setVisible(true); // shows label3
    t.setDelay(2000); // etc...
    }
    Instead, you could write your listener to know how many times it's been called, and only setVisible the N-th label (that is only one suggestion, there are various other ways).
    Regards,

    J.
  • 6. Re: How to add time event to show jframe component one after another sequetial?
    831755 Newbie
    Currently Being Moderated
    Thanks for your help!

    What I understand is, I take one time I should take one counter like loopslot in here ...
    suppose I have 6 lables then


    if(count==0)
    {
    jLabel2.setVisible(true);
    t.setDelay(delay);
    }
    else if(count==1)
    {
    jLabel3.setVisible(true);
    t.setDelay(delay);
    }
    else if(count==2)
    {
    jLabel4.setVisible(true);
    t.setDelay(delay);
    }
    t.restart();
    //This also does not work. only lable2 appear, rest not!

    what's use of animator.repaint() i dont know. Do I need to make use of it? I tried but animator object not available to me.
    Can you tell me whether this can be done using applet easily?
  • 7. Re: How to add time event to show jframe component one after another sequetial?
    jduprez Pro
    Currently Being Moderated
    Do you increment your variable count ? :o)

    More seriously, for better help sooner, please post an SSCCE (http://sscce.org).
    Please make sure to understand how to post code here beforehand - use {noformat}
    {noformat} tags!
    
        J.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 8. Re: How to add time event to show jframe component one after another sequetial?
    831755 Newbie
    Currently Being Moderated
    On this page only documentation is there,
    I didnt find forum link on this page.
  • 9. Re: How to add time event to show jframe component one after another sequetial?
    EJP Guru
    Currently Being Moderated
    On this page only documentation is there
    So did you read it?
    I didnt find forum link on this page.
    Did someone say there was a forum link there?
  • 10. Re: How to add time event to show jframe component one after another sequetial?
    831755 Newbie
    Currently Being Moderated
    Your critics >:o !!!

    Anyways it helped me too get it done!!
    I was wrong and careless about your reply :P...

    anyways I incremented count ...
    thanks buddy

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points