Results 1 to 5 of 5
Thread: wait + repaint. Can't understand
- 11-10-2009, 05:17 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
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.
- 11-10-2009, 05:41 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Yes, you do because that is whats causing your problem.and I don't need a separate thread
Read the section from the Swing tutorial on Concurrency for an explanation and solution.
- 11-11-2009, 08:14 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
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?
-
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.
- 11-11-2009, 04:40 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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().In my main method i put this code:
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
-
wait() and notify() problems
By greyradio in forum Threads and SynchronizationReplies: 1Last Post: 08-03-2009, 03:36 AM -
about wait() and notifyALL
By denis in forum Threads and SynchronizationReplies: 13Last Post: 04-22-2009, 08:28 AM -
Thread Wait
By jiexx in forum Threads and SynchronizationReplies: 1Last Post: 03-19-2009, 05:26 PM -
What is the execution path of wait() and notify() ?
By AegisCruiser in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:16 PM -
How to use sleep() to wait for a while
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks