Question

The code below works great for when the user assigns the total amount of stat points I allocate. However, I would love to only disable the plus button, so they could lower a stat value and then add again.

if ((strengthModel.getNumber().intValue()) + (constitutionModel.getNumber().intValue()) + (dexterityModel.getNumber().intValue()) + (intelligenceModel.getNumber().intValue()) > 49){
     strengthSpinner.setEnabled(false);
     constitutionSpinner.setEnabled(false);
     dexteritySpinner.setEnabled(false);
     intelligenceSpinner.setEnabled(false);
}

Is that possible with int spinners? I did not see it in the docs.

EDIT Bit more info: You can spread your stat points around or assign them all to one stat. The max on each of the models is all 10 of the un-used points.

Was it helpful?

Solution

For anyone who finds this thread here is how I solved my issue:

public void stateChanged(ChangeEvent e) {
    Component[] components = characterCreationPanel.getComponents();
    Component component = null; 
    strengthValue = strengthModel.getNumber().intValue();
    constitutionValue = constitutionModel.getNumber().intValue();
    dexterityValue = dexterityModel.getNumber().intValue();
    intelligenceValue = intelligenceModel.getNumber().intValue();
    for (int i = 0; i < components.length; i++)
    {
        component = components[i];
        if (component instanceof JLabel){
            if (((JLabel) component).getText().substring(0, 5).equals("Stat ")){
                ((JLabel) component).setText("Stat Points Left: " + Integer.toString(50 - (strengthValue + constitutionValue + dexterityValue + intelligenceValue)));
                if ((strengthValue + constitutionValue + dexterityValue + intelligenceValue) == 50){
                    System.out.println("Hit your cap.");
                }
            }       
        }
        strengthModel.setMaximum(50 - (constitutionValue + dexterityValue + intelligenceValue));
        constitutionModel.setMaximum(50 - (strengthValue + dexterityValue + intelligenceValue));
        dexterityModel.setMaximum(50 - (strengthValue + constitutionValue + intelligenceValue));
        intelligenceModel.setMaximum(50 - (strengthValue + constitutionValue + dexterityValue));
    }
}

Thanks "ziesemer" for the tip with the setMaximium.

OTHER TIPS

Create your JSpinner with a SpinnerNumberModel with the desired minimum and maximum values.

Add a change listener to each model, such that as each spinner is changed, the sum of all your spinners is calculated, and then the maxiumum on each spinner set to its current value to disable the plus button if necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top