wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Refresh problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Refresh problem" Watch "Refresh problem" New topic
Author

Refresh problem

Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
Hello, i can't get the solution to refresh the label while increasing value "i" and display correctly, because when i click the button "aceptar" it display only the result. how can i refresh the label so i can see the increasing.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
It is impossible to try your code because you did not supply all the classes; what are RowSpec and FormFactory?
What happens when you click the button? The loop in the actionPerformed method will run quickly, maybe 5 iterations to 1μs. But it may take as long as 10ms for the display to refresh, so the loop can almost complete in that time.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Also, the loop is running inside the event thread which means the update of the label display only takes place when the loop has finished (no matter how long the loop takes). One will need to run the loop in a separate thread and update the display using SwingUtilties.invokeLater() to stand a chance of seeing the change happening and one will need to slow the loop down to see every update.
Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
Campbell Ritchie wrote:It is impossible to try your code because you did not supply all the classes; what are RowSpec and FormFactory?
What happens when you click the button? The loop in the actionPerformed method will run quickly, maybe 5 iterations to 1μs. But it may take as long as 10ms for the display to refresh, so the loop can almost complete in that time.


Exactly, when i click the button i almost have the result at the instant, but if i increment the for loop, the screen freeze until complete the for bucle and show the message, the RowSpec and FormFactory are clases created by the WindowsBuilder for the componentes. Thats why i need to refresh the windows each time the for iteration complete a loop, so i can
appreciate the incresing of the variable. If you don't understand something please tell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
It is because of the difference in speed between the loop and the display. As Richard Tookey says, you would have to run the loop in a separate thread, with a delay (try Thread.sleep()) after each iteration, to allow the display to change between iterations. But since Swing is not thread‑safe, I can foresee lots of other possible problems if you start accessing Swing components from outwith the event despatch thread.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Campbell Ritchie wrote:I can foresee lots of other possible problems if you start accessing Swing components from outwith the event despatch thread.


Which is why one uses SwingUtilities.invokeLater() !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Of course! I ought to wake up sometimes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refresh problem