Results 1 to 5 of 5
  1. #1
    cob
    cob is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default how to refresh frame during thread

    hey everyone,

    basically what im doing is im running a thread which implements a while statement. within this while statement an action is performed and then the thread sleeps for a short period of time before continuing. everything works fine except the frame doesnt refresh until the thread is complete, does anyone have any ideas on how i can update it everytime it sleeps?

    here is the code of the thread in case it helps:

    Java Code:
    public class autoSlow extends Thread
    {
            public void run()
            {
                    while(ballResult!=5)
                    {
                            ball.doClick();
                            try
                            {
                                    this.sleep(600);
                            } catch (InterruptedException e)
                            {}
                    }
            }
    }
    thanks in advance, cob
    Last edited by sunde887; 08-07-2011 at 09:40 AM. Reason: Added code tags, [code]...[/code]

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

    Default

    Go through the tutorial on Concurrency in Swing.

    also, the code snippet you posted doesn't reveal the real problem. Additionally, read the documentation for Runnable:
    In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
    db

  3. #3
    cob
    cob is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    hmmmmm i went through the tutorial and a swing worker thread seems to be what i want, but i have no idea what to publish seeing as my repaint methods aren't threads, they're methods. any idea of what i could write instead of what ive got above that performs the same method but repaints after each sleep?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    my repaint methods aren't threads,
    All code executes on a thread. The Thread class allows you to create additional threads as needed.

    If you have changed the GUI and what the paintComponent method called to re draw something, call the repaint() method.

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

    Default

    Judging from your OP
    an action is performed and then the thread sleeps for a short period of time before continuing
    you need a javax.swing.Timer, not a SwingWorker.

    db

Similar Threads

  1. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  2. how to open one frame to another frame
    By tukadiya in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2010, 08:27 PM
  3. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  4. Replies: 7
    Last Post: 11-02-2009, 06:31 PM
  5. 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

Posting Permissions

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