This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refresh problem

 
Hernan Tavella
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49781
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49781
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49781
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course! I ought to wake up sometimes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic