Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel wont update when i want it to  RSS feed

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple game to practice using web services. Basically a user playing against the computer in a dice roll game.
The game works fine but i want to add a message to say whos turn it is so "your turn" or "computer turn".
For some reason the JLabel wont update when i want it to. As you can see in the code below, if the users rolls a 1 then i want to set my JLabel, get the user total, let the computer play the game, update the computers score and then set my JLabel back again.
On the server end the computer will roll between 1 and 3 times with a sleep after each roll of 1/2 a second. This gives enough time for the server turn message to display and not to long that it slows the game down.
If i just keep the server message in the code below then the JLabel will update but only after the server has rolled. How can i get it to execute in the order i want it to, and display my messages when i want them?

 
Campbell Ritchie
Sheriff
Posts: 53766
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something peculiar about that. How are you separating the display (if you use the MVC pattern, that is the V=view) from the data (in MVC M=model)? I am sure you should have one method which sends the “my turn” data to the app, and it doesn’t matter whether the app is on the same computer or 10000 miles away. You should have another method which is called when the app sends back the data.
Where have you got the Thread.sleep() call? How will that help?
 
Rob Camick
Ranch Hand
Posts: 2754
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like your code is executing on the EDT and when you sleep you prevent the GUI from updating itself.

Your Server code should execute on a separate Thread so you don't block the EDT.

Read the Swing tutorial on Concurrency for more information.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made some changes below to put the updating of the labels into separate methods.
The code i have showed is part of an actionListener on my two buttons "Roll" and "Hold"
When Roll is pressed i check whether it equals 1 if so its then the servers turn to roll.
The Thread.sleep() is in the serverRoll() method of pigGameProxy() which is my web service.
I didn't think that would effect my GUI as the updating of the Label is done before and after the serverRoll method is called.
I have added println()'s to my new methods serverStartView() and serverEndView() and they print out correctly. I could try a new thread and see if that solves the issue though.

Also Campbell the Thread.sleep() call is just to add a small pause to the game so the user can see that the server is taking their turn.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!