Closed Thread
Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Delay ..~

  1. #11
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Delay ..~

    If you do the sleep before the screen has a chance to refresh, that will happen. You may need to look into forcing the screen to refresh, first.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    abdul.gafur's Avatar
    abdul.gafur is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Salo, Riau, Indonesia
    Posts
    42
    Rep Power
    0

    Re: Delay ..~

    Could you tell me how to refresh it?

  4. #13
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Delay ..~

    Unfortunately, I'm just learning Java myself, and haven't gotten to GUI stuff yet.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #14
    R3.RyozKidz Guest

    Re: Delay ..~

    Shouldn't it will automatically refresh after the changing of the button text ?
    Can we just trace the time of refreshing the GUI?

  6. #15
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Delay ..~

    The answer to this is to simply not pause execution at all. Remember that Swing is not thread safe, so making changes to it directly from a thread can be dangerous. Anyway, to me it looks like a java.util.Timer is just fine for this task. I tried out writing in a Timer that runs a subclass of TimerTask, and it looks like it works (though it's still buggy...). These are the modifications I made:
    Code:
    import java.util.Timer;
    //...
                if(toggleButton == 2) 
                { 
                    Object source = event.getSource(); 
                     
                    for(int i = 0 ; i < buttons.length ; i++) 
                    { 
                        if(source == buttons[i]) 
                        { 
                            secondEvent = i; 
                        } 
                    } 
                     
                    buttons[secondEvent].setText(labels[secondEvent]); 
                    buttons[secondEvent].setEnabled(false); 
                    
                    timer = new Timer();
                    timer.schedule(new java.util.TimerTask() {
                        public void run()
                        {
                            javax.swing.SwingUtilities.invokeLater(
                                new ButtonShower(buttons, firstEvent, secondEvent)
                            );
                        }
                    }, 1000);
                    
                    toggleButton = 0;
                     
                    if(pairMatched == 8 ) 
                    { 
                        JOptionPane.showMessageDialog(null , "Congratulation !\nYou have won!" , "Message" , JOptionPane.PLAIN_MESSAGE); 
                    } 
    //...
    // Within the ActionEventHandler class:
            private class ButtonShower implements Runnable
            {
                public ButtonShower(JButton[] buts, int but1, int but2)
                {
                    this.buts = new JButton[] { buts[but1], buts[but2] };
                }
    
                public void run()
                {
                    if (buts[0].getText().equals(buts[1].getText())) 
                    {
                        pairMatched++;
                    }
                    else 
                    { 
                        for (JButton but : buts)
                        {
                            but.setText("*");
                            but.setEnabled(true);
                        }
                    }
                }
    
                private JButton[] buts;
            }
    Remember to use javax.swing.SwingUtilities.invokeLater(new Runnable()) every time you modify the GUI from outside the Event Dispatch Thread!
    Wow I changed my sig!

  7. #16
    R3.RyozKidz Guest

    Re: Delay ..~

    ok ... i will try this out .. thanks for the help ...

Closed Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. delay function in C in number of seconds
    By nerio in forum C and C++
    Replies: 4
    Last Post: 07-23-2011, 10:18 PM
  2. Delay a function execution
    By ferovac in forum JavaScript and CSS
    Replies: 3
    Last Post: 05-27-2011, 08:45 AM
  3. Delay/timer? (other that Sleep();
    By TeenChristian in forum C and C++
    Replies: 72
    Last Post: 07-20-2010, 12:44 PM
  4. how to add in a delay?
    By shifu in forum C and C++
    Replies: 8
    Last Post: 01-29-2010, 02:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

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