3

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.

Dov
  • 15,530
  • 13
  • 76
  • 177
KisnardOnline
  • 653
  • 4
  • 16
  • 42

2 Answers2

4

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.

KisnardOnline
  • 653
  • 4
  • 16
  • 42
3

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.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • The models all have a max that allows for all 10 "unused" points, because that is how the stat allocation should work. You can assign all of your stat points to one stat or spread them out. strengthModel = new SpinnerNumberModel(10, 10, 20, 1); – KisnardOnline Nov 29 '11 at 04:52
  • Sorry, I didn't read the question closely enough. Edited my answer to include some advice that should help you. – ziesemer Nov 29 '11 at 04:56
  • I'm sorry for even asking, but is there any way you can expand on how about of achieve that?? I, admittedly, am not the best coder. – KisnardOnline Nov 29 '11 at 05:10
  • This [Q&A](http://stackoverflow.com/questions/7617605/java-jslider-precision-problems) addresses a similar problem using a collection slider models. A similar approach may be adapted to this problem using a collection of spinner models. – trashgod Nov 29 '11 at 05:31