Results 1 to 5 of 5
  1. #1
    Golodh is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default wait + repaint. Can't understand

    Hi,

    I'm writing code to control a mechanical device which takes some time to do its job. I have to run a given number of different operations on the machine, and at the end of each of these operations I must update an extension of JPanel with data from the machine, then start the next one and so on.

    As I can't predict how much time the machine will take, I must wait untill i get the desired measure from it, then modify my Jpanel and repaint it. An example below:

    int x = 0;
    double t0, t1;
    startOp1();
    t0 = System.currentTimeMillis();
    while (x <= 10) {
    x = readFromMachine();
    }
    t1 = System.currentTimeMillis();
    updateMyJPanel(t1 - t0);
    MyJPanel.repaint();

    int y = 0;
    startOp2();
    t0 = System.currentTimeMillis();
    while (y <= 100) {
    y = readFromMachine();
    }
    t1 = System.currentTimeMillis();
    updateMyJPanel(t1 - t0);
    MyJPanel.repaint();

    ... and so on ...


    As a result, my program runs the machine correctly but the GUI is not responsive until the end. I read a lot about threading and EDT but I can't apply the things I read to my case, and I don't need a separate thread.

    What can I do?
    Last edited by Golodh; 11-10-2009 at 05:33 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    and I don't need a separate thread
    Yes, you do because that is whats causing your problem.

    Read the section from the Swing tutorial on Concurrency for an explanation and solution.

  3. #3
    Golodh is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    I still don't get it.

    In my main method i put this code:


    SwingUtilities.invokeLater(new Runnable() {
    public void run() {

    init();
    GUI.setViewPanel("main");
    }
    });


    Where init() and setViewPanel("main") are some custom method for initialization and displaying of my GUI.

    But again if I work with delays and GUI elements I still get the same behavior. What am I doing wrong?
    Could I have a complete and simple code example please?

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

    Default

    Did you read the link provided by camickr above? From your current post, it looks as if you didn't, and I will venture that you won't solve this problem til you do. Much luck.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    In my main method i put this code:
    Which defeats the whole purpose. You need to start a Thread for your long running task. In that thread, the pieces of code that update the GUI need to use the invokeLater().

    There are plenty of examples on the web for using Threads and invokeLater. I've introduced you to the classes and methods for do a search since you now know which keywords to use.

    If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Similar Threads

  1. wait() and notify() problems
    By greyradio in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 03:36 AM
  2. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 08:28 AM
  3. Thread Wait
    By jiexx in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 05:26 PM
  4. What is the execution path of wait() and notify() ?
    By AegisCruiser in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:16 PM
  5. How to use sleep() to wait for a while
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:32 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
  •