Petr Cina
Petr Cina

Reputation: 21

Clear button only makes counter JLabel blank but does not reset it.

I have made this application:

image

For example when I click on the clear button when the counter JLabel (pointsAvailable) is 19 then the counter JLabel goes blank as expected, however when I start adding points again it starts from 19 not 40 as set on the start. I would like to make it to reset back to 40 instead of just making it blank

Code for the clear button

private void JButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    speedPoints.setText("");
    attackPoints.setText("");
    defencePoints.setText("");
    powerPoints.setText("");
    agilityPoints.setText("");
    focusPoints.setText("");
    availablePoints.setText("");
}  

Code for Jlabel counter

    public class addingPointsUI extends javax.swing.JFrame {
int pointsAvailable=40;
        int speed=0;
        int power=0;
        int focus=0;
        int agility=0;
        int defence=0;
        int attack=0;

Code for buttons +/-: to allow me to add or decrease value "example power - button"

private void powerMinusActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (power > 0 ){
        if (pointsAvailable <= 0) {
    JOptionPane.showMessageDialog(null, "You are out of available points");
    return;
        }
        power = power - 1;

        pointsAvailable = pointsAvailable +1;

        availablePoints.setText(String.valueOf(pointsAvailable));

        powerPoints.setText(String.valueOf(power));

    }else {

            JOptionPane.showMessageDialog(null,"You cannot take anymore points from Power");
    }
}

Thank your for your kind replies.

Upvotes: 1

Views: 67

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Use a JSpinner with SpinnerNumberModel. Change the value of the model. The component will update and further changes will act on the current value of the model.

Upvotes: 1

Petr Cina
Petr Cina

Reputation: 21

Found my own solution on google:

private void ClearActionPerformed(java.awt.event.ActionEvent evt) {     
speedPoints.setText(String.valueOf(0));
powerPoints.setText(String.valueOf(0));
agilityPoints.setText(String.valueOf(0));
defencePoints.setText(String.valueOf(0));
focusPoints.setText(String.valueOf(0));
attackPoints.setText(String.valueOf(0));
availablePoints.setText(String.valueOf(40));
 }                                     

Works perfectly

Upvotes: 0

Jetmir Berisha
Jetmir Berisha

Reputation: 1

I can quickly think of two solutions: 1. In the event handler of your clear button include the following:

private void JButtonActionPerformed(ActionEvent evt){
    ...
    pointsAvailable=40;
    speed=0;
    power=0;
    focus=0;
    agility=0;
    defence=0;
    attack=0;
}

This will reset all of your stats. 2. Or you can add an if statement to the listeners of every button that adds or subtracts a stat that will check if the specific statistic is empty. For example, for the speed buttons the code would look like so:

if (speedPoints.getText() == ""){
    pointsAvailable += speed;
    speed = 0;
}

Upvotes: 0

Related Questions