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

Very new to java: How to update a label  RSS feed

 
david marquez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm stuck on how to update a label. The program I'm creating is a basic snakes and ladders game. I have got as far as generating two random numbers (ie two dice) and giving the user the option of selecting which amount they want to move by. This is done by clicking one of two buttons. The values for each die are displayed in labels. The buttons are created in a seperate class to the dice and the labels. The problem I'm having is updating the text in the labels to show the newly generated number after one of the buttons is clicked. I know it is generating a new number because when i click one of the buttons for the second time, it moves by a new amount. Here are the relevant parts of code:



---------------------------------------------------------------------------------------------------------



Any help would be appreciated, Thank you for your time!
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

We usually discuss that sort of problem on the Swing forum, whither I shall move you. Please find the CODE button to maintain indentation; I have edited your post so you can see how much better it looks.

I haven't had enough time to read the whole of the code, but it seems peculiar that you are creating new Dice objects every time the actionPerformed methods are invoked. I don't think setting a value in the constructor looks correct; leave it blank. You would do better to have a method which calls the random generator and resets the text in the JLabels whenever those actions are performed. Same object, different state (unless you happen to get the same dice twice).
 
david marquez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks alot for your help. Please be patient as I'm very new to this..

So removing the single line from the button code would make it look like this:

.

..But how would i go about creating a method to call the random generator and reset the JLabel text?

Thanks again!
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Alea jacta est" is Latin for "the die is cast."

The code shown may require a little refinement before it actually works! Or even a lot of refinement.
 
david marquez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant! Exactly what it needed. It's running perfect now. Thanks alot for all your help!
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!