• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reset Button

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating a simple game in Netbeans but I've come across a problem with my reset button which when I click on it, it only clear the text in the label but doesn't add it back to Total Coins?

My code for the reset button,



 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

L Ze wrote:(...) it only clear the text in the label but doesn't add it back to Total Coins?


There is no code in your method that could add anything to "Total Coins" so how can you expect something to be added to "Total Coins"?
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Welcome to the Ranch!

L Ze wrote:(...) it only clear the text in the label but doesn't add it back to Total Coins?


There is no code in your method that could add anything to "Total Coins" so how can you expect something to be added to "Total Coins"?


Hi Paweł, thanks for the welcome but I'm not sure how to do the add code statement as I'm a beginner and only followed this guide for the reset button
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Ze wrote:

Paweł Baczyński wrote:Welcome to the Ranch!

L Ze wrote:(...) it only clear the text in the label but doesn't add it back to Total Coins?


There is no code in your method that could add anything to "Total Coins" so how can you expect something to be added to "Total Coins"?


Hi Paweł, thanks for the welcome but I'm not sure how to do the add code statement as I'm a beginner and only followed this guide for the reset button

I think we'd have to see more of your code.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All your code does is clear the text of the coins widget, which I suspect is a JLabel. You probably have a variable for total coins somewhere that you use to display the total (495 in your example). You should set total coins to zero, then set the coins label's text to "Total Coins" + totalCoins

If that doesn't work, post more code for us to look at.
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:All your code does is clear the text of the coins widget, which I suspect is a JLabel. You probably have a variable for total coins somewhere that you use to display the total (495 in your example). You should set total coins to zero, then set the coins label's text to "Total Coins" + totalCoins

If that doesn't work, post more code for us to look at.


 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, my suggestion was correct, except that totalCoins should be totalcoins.

Note: don't post a screen shot of your code. Instead, copy the text and UseCodeTags (← that's a link).
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like you'd want to set totalcoins to 500 and ocoins to zero. That would reset you back to the starting state. Also, you'd have to reset the matching text fields as well.
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe have a look at JavaFX. Learning Swing/AWT nowadays is not very useful. It's outdated.
Unfortunately, in some countries Java 5 en 6 are pretty common, so it cant' be used.
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:All your code does is clear the text of the coins widget, which I suspect is a JLabel. You probably have a variable for total coins somewhere that you use to display the total (495 in your example). You should set total coins to zero, then set the coins label's text to "Total Coins" + totalCoins

If that doesn't work, post more code for us to look at.


I need total points to be 500 instead of 0 so I can take coins off it


Carey Brown wrote:Seems like you'd want to set totalcoins to 500 and ocoins to zero. That would reset you back to the starting state. Also, you'd have to reset the matching text fields as well.


Yes, the total coins is 500 and ocoins is 0 as that's the starting point, each time I press the add button, it will take a coin from the total points and add it to ocoin label however I don't know the missing code that should be under this code so once I press the Reset button, it will add all the points I have taken back to total points,

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Ze wrote:

Carey Brown wrote:Seems like you'd want to set totalcoins to 500 and ocoins to zero. That would reset you back to the starting state. Also, you'd have to reset the matching text fields as well.


Yes, the total coins is 500 and ocoins is 0 as that's the starting point, each time I press the add button, it will take a coin from the total points and add it to ocoin label however I don't know the missing code that should be under this code so once I press the Reset button, it will add all the points I have taken back to total points,

You don't need to 'add' all the points back you just need totalcoins=500.
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

L Ze wrote:

Carey Brown wrote:Seems like you'd want to set totalcoins to 500 and ocoins to zero. That would reset you back to the starting state. Also, you'd have to reset the matching text fields as well.


Yes, the total coins is 500 and ocoins is 0 as that's the starting point, each time I press the add button, it will take a coin from the total points and add it to ocoin label however I don't know the missing code that should be under this code so once I press the Reset button, it will add all the points I have taken back to total points,

You don't need to 'add' all the points back you just need totalcoins=500.


I get what you mean now, so once I press the Reset/Clear button, it will return back to the total coins value which is 500 but when I try it, it clears the ocoins label but total coins doesn't reset back to 500, am I missing something?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your code. We don't know what "when I try" means.

Just as a guess, coins.setText("") does not do anything to ocoins or totalcoins.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After setting totalcoins to 500 you'll need to call

in order to update the display.
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Post your code. We don't know what "when I try" means.

Just as a guess, coins.setText("") does not do anything to ocoins or totalcoins.


I mean that when I run the project to test the code I posted earlier on, the total coins value doesn't return to 500 when I press the clear button but only clears the ocoins label for example, I've 10 in the ocoins label when I click clear the total coins value is at 490 but it should be at 500

Carey Brown wrote:After setting totalcoins to 500 you'll need to call

in order to update the display.


I've changed the Reset button code to the one below but still doesn't update/reset back to 500?

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Ze wrote:I mean that when I run the project to test the code I posted earlier on, the total coins value doesn't return to 500 when I press the clear button but only clears the ocoins label for example, I've 10 in the ocoins label when I click clear the total coins value is at 490 but it should be at 500



Look in btnaddActionPerformed() or btnminusActionPerformed(). You are adding or subtracting from totalcoins and ocoin, then displaying those values in the lblpoints and coinpoints labels. This is what you need to do in the resetActionPerformed() code, except that ocoin is cleared and totalcoins is set to 450.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:After setting totalcoins to 500 you'll need to call

in order to update the display.


I don't see where you are setting totalcoins.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steps:
1) set totalcoins to 500
2) set lblpoints text to new value of totalcoins
3) post your changes
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:After setting totalcoins to 500 you'll need to call

in order to update the display.


I don't see where you are setting totalcoins.


I've a integer at the top to set totalcoins = 500

Carey Brown wrote: Steps:
1) set totalcoins to 500
2) set lblpoints text to new value of totalcoins
3) post your changes


This code lblpoints.setText(String.valueOf(totalcoins)); is to set lblpoints text to new value of total coins?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Ze wrote:I've a integer at the top to set totalcoins = 500


But you need it again in the resetActionListener() method, or it won't be reset when you push the button.

This code lblpoints.setText(String.valueOf(totalcoins)); is to set lblpoints text to new value of total coins?


Not quite. It sets the text of lblpoints to the current value of totalcoins.
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:

L Ze wrote:I've a integer at the top to set totalcoins = 500


But you need it again in the resetActionListener() method, or it won't be reset when you push the button.

This code lblpoints.setText(String.valueOf(totalcoins)); is to set lblpoints text to new value of total coins?


Not quite. It sets the text of lblpoints to the current value of totalcoins.


I think I'm nearly there, I've placed this code lblpoints.setText(String.valueOf(totalcoins = 500)); in the Reset button and it does reset back to 500 when I click on the reset button however the ocoins label doesn't reset and when I click on the add button it will add onto the previous reset number





 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Ze wrote:

Knute Snortum wrote:

L Ze wrote:I've a integer at the top to set totalcoins = 500


But you need it again in the resetActionListener() method, or it won't be reset when you push the button.

This code lblpoints.setText(String.valueOf(totalcoins)); is to set lblpoints text to new value of total coins?


Not quite. It sets the text of lblpoints to the current value of totalcoins.


I think I'm nearly there, I've placed this code lblpoints.setText(String.valueOf(totalcoins = 500)); in the Reset button and it does reset back to 500 when I click on the reset button however the ocoins label doesn't reset and when I click on the add button it will add onto the previous reset number

Do the exact same thing for ocoin.
 
L Ze
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

L Ze wrote:

Knute Snortum wrote:

L Ze wrote:I've a integer at the top to set totalcoins = 500


But you need it again in the resetActionListener() method, or it won't be reset when you push the button.

This code lblpoints.setText(String.valueOf(totalcoins)); is to set lblpoints text to new value of total coins?


Not quite. It sets the text of lblpoints to the current value of totalcoins.


I think I'm nearly there, I've placed this code lblpoints.setText(String.valueOf(totalcoins = 500)); in the Reset button and it does reset back to 500 when I click on the reset button however the ocoins label doesn't reset and when I click on the add button it will add onto the previous reset number

Do the exact same thing for ocoin.


ocoins.setText(String.valueOf(totalcoins = 500)); only set ocoins value to 500 but doesn't reset it?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Ze wrote:

Carey Brown wrote:

L Ze wrote:

Knute Snortum wrote:

L Ze wrote:I've a integer at the top to set totalcoins = 500


But you need it again in the resetActionListener() method, or it won't be reset when you push the button.

This code lblpoints.setText(String.valueOf(totalcoins)); is to set lblpoints text to new value of total coins?


Not quite. It sets the text of lblpoints to the current value of totalcoins.


I think I'm nearly there, I've placed this code lblpoints.setText(String.valueOf(totalcoins = 500)); in the Reset button and it does reset back to 500 when I click on the reset button however the ocoins label doesn't reset and when I click on the add button it will add onto the previous reset number

Do the exact same thing for ocoin.


ocoins.setText(String.valueOf(totalcoins = 500)); only set ocoins value to 500 but doesn't reset it?

Of course it won't, you're setting totalcoins=500 inside it. It might be clearer for you if you broke this into two lines.
Edit: And it's lblpoints.setText(). You have to keep track of the type of each variable. setText() only works on JLabel.
Edit2: Sorry, 'coinpoints'.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you post your next question include the code for the reset method as it stands when you ask the question. Remember, cut and paste the code into the question and apply 'Code' tags.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think one of your difficulties comes from your variable names. For instance, your "lblpoints" variable would be clearer if it were "totalCoinsLabel".
Also, instead of compound expressions like:

You might break it into separate lines, starting from the inner parenthesis out.

Now, go line by line and make sure you understand what each line is telling the program to do and why the order of these lines is important.
 
reply
    Bookmark Topic Watch Topic
  • New Topic