Texfields updating all at one at the end.

Hey.

Im trying to get a series of Textfields to update their values using textfield.setText(""); The setText's are within a function that draws a line, then calls the setText, then pauses for a second (either in a for loop, or a thread.sleep), then repeats the process multiple times, drawing new lines and seting different textfields.

The probem im having is that the lines are drawn in sequence with the correct spacings, but the textfields are only updated, as one mass right at the end.

I want them updated as the lines are drawn. Here is some of my code:

public void animate() {

System.out.println("in cheese");

comp2D.setColor(Color.red);

drawBresenhamLine one = new drawBresenhamLine(comp2D, 70, 190, 150, 110);

Layout6.tables.vertex.no1.setText("99");

Layout6.sleep();

comp2D.setColor(Color.black);

drawBresenhamLine two = new drawBresenhamLine(comp2D, 70, 190, 150, 110);

comp2D.setColor(Color.red);

drawBresenhamLine three = new drawBresenhamLine(comp2D, 150, 110, 150, 190);

Layout6.tables.vertex.x1.setText("50");

Layout6.sleep();

comp2D.setColor(Color.black);

drawBresenhamLine four = new drawBresenhamLine(comp2D, 150, 110, 150, 190);

comp2D.setColor(Color.red);

drawBresenhamLine five = new drawBresenhamLine(comp2D, 150, 190, 70, 190);

Layout6.tables.vertex.y1.setText("33");

Layout6.sleep();

comp2D.setColor(Color.black);

drawBresenhamLine six = new drawBresenhamLine(comp2D, 150, 190, 70, 190);

comp2D.setColor(Color.red);

drawBresenhamLine seven = new drawBresenhamLine(comp2D, 150, 110, 320, 110);

IS there a way to force the panel that holds the texfields to update, or to force the textfields to update.

I have tried the above code in many ways, including calling the setText within the panel that holds the texfields, but it did not change anything. It will only ever update at the end of public void animate.

Thanks

Mark

[2029 byte] By [Chaney_Student2a] at [2007-11-27 0:26:55]
# 1
It looks like your text field's are updating properly when you call setText however your JPanel most likely doesn't finish painting to the screen until your method completes.Try calling the repaint() method of your JPanel after each call to setText.
maple_shafta at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 2

Nope. That didnt work.

I have created a paint method in the panel that draws the lines.

However there isnt a paint method in the panel that holds the texfields.

I tried repaint(), this just repainted the panel that holds the lines

I also tried Layout6.tables.vertex.repaint();..........this should have repainted the panel with the textfields, but once again they only appeared once the full animate() function had completed.

Chaney_Student2a at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 3
I am curious why you would need to call sleep within your animate method.That may be the reason why you experience a delay in the repainting of the panel containing the text fields.
maple_shafta at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for your help so farI call sleep, so that it adds a pause between drawing objects. I have tried thread.sleep and i have tried a simple "for" loop. Both give the same result of the textfields updating all in one go at the end.
Chaney_Student2a at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 5

It sounds like the animate method is being called from the AWTEventQueue thread. To find this out, put this in the method:

System.out.println(Thread.currentThread().getName());

This would be the case if you are calling animate from an action listener, like a button press or something. What I suspect is happening, is that the AWTEventQueue is in this code, and you are telling it to update the text fields. What really happens, is the text gets updated internally, and a request gets added to the AWTEventQueue to repaint that component. Since the AWTEventQueue thread is the thread processing that queue, and it is currently stuck in this method, it will not update the text fields until it gets to those requests in the queue.

Secondly, if this is the case, having sleep calls in here is really bad. It would essentially lock up the GUI the entire time you are in this method. This method will probably need to be in another thread, which could lead to other problems if you are new to threads.

robtafta at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 6
Why not try using a javax.swing.Timer thread instead of calling sleep?Calling the sleep method like that will freeze ALL execution in the current thread.Having your panel objects being drawn on a seperate thread with a set delay will prevent this delay in repainting the
maple_shafta at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 7

I Tried your sugestion and it outputed that the Queue was 0.

In terms of locking the GUI, that doesnt mater at all. Or at least it wont mater for this bit.

I havent really used threads at all. And have been warned they are very very dificult to use and that you should avoid them at all costs (however, if thats the obly solution) .

Currently im not using any threads, im using a for loop.

I also looked into the timer, but couldnt not find out how to implement it. However i will have another look.

Cheers for your help. I will have to loon into threads some more :( Unless there is anyway of forcing the queue to execute the update panel without threads?

Thanks

Mark

Chaney_Student2a at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 8
Animate is indeed being called from an actionListener, well technically an ActionPerformed method.
Chaney_Student2a at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 9

So the quick, yet possibly unstable solution is to call animate in a new thread is to do this:

new Thread() {

public void run() {

animate();

}

}.start();

This will solve your problem, but adds a new one. If that is tied to a button, you will end up with a ton of threads if someone presses the button over and over. A simple way to prevent this is like so:

boolean animating = false;

.......

if (!animating) {

animating = true;

new Thread() {

public void run() {

animate();

animating = false;

}

}.start();

}

Although it is somewhat of a hack, it should work and be relatively safe to use.

robtafta at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...
# 10
You Legend. It works, well tried the first bit of it, but im sure the second bit works.Cheers mutchly
Chaney_Student2a at 2007-7-11 22:26:02 > top of Java-index,Java Essentials,Java Programming...