0

I apologize in advance if this has been asked elsewhere, but I have looked around and I can't seem to find an answer that fits my situation.

Some background: I have taken it upon myself to write a sort of "RPG character builder" for the Savage Worlds RPG system. I am using the NetBeans IDE to design a UI for this application. So far, things have gone pretty smoothly, but I am now encountering an error I can't seem to figure out.

I have a set of Attributes: Agility, Strength, Smarts, Spirit, and Vigor. The values for those attributes are a Die-type, ie d4, d6...d12. I created an Enum to represent these die types:

public enum DieType {

    D4(4), D6(6), D8(8), D10(10), D12(12);

    private int face;

    private DieType(int face) {
        this.face = face;
    }

    public int getValue() {
        return face;
    }

    @Override
    public String toString() {
        String dieType = "d" + face;
        return dieType;
    }

}

In my UI, I created a series of JSpinners to be able to adjust the values for each attribute. I wanted to use those die types in the spinners, so I initialized a new SpinnerListModel for them:

agiSpin.setModel(new SpinnerListModel(DieType.values()));

So far, so good, everything is still working well, and I can run the application and the window appears with the Enum list populated for each of the spinners. (Planning to move the model initialization to a variable that I can plug into each individual spinner, but right now I am working with NetBeans-generated code, so am creating each instance separately).

The problem occurs when I attempt to intialize a value for each spinner based on an instance of my SWCharacter class, instantiated before the GUI is drawn. For the above agiSpin spinner, I do as such: agiSpin.setValue(swchar.getAgiAttr()); in order to assign the appropriate value. The variable agiAttr is an instance of the DieType Enum. When I run the program, I get:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: invalid sequence element
at javax.swing.SpinnerListModel.setValue(SpinnerListModel.java:185)
at javax.swing.JSpinner.setValue(JSpinner.java:356)

...originating from that attempt to set the value.

What am I doing wrong with this? Do I need to create the List Model in a different way? Is there a syntax error in the way I'm attempting to assign values?

amthomas2112
  • 47
  • 1
  • 1
  • 10

2 Answers2

1

A Spinner is usually used for integer or double values that the user inputs. In this case, I would use a ComboBox in which each value is initialized. The code to create a JComboBox would be:

    JComboBox die = new JComboBox<DieType>(DieType.values());

This should work. In NetBeans, it automatically creates the ComboBox, so you will need to right click and click "Customize Code" and make the necessary changes like above.

  • I thought about the ComboBox idea, but thought it would be better to have the spinner increment/decrement functionality. I was under the impression from the API that the SpinnerListModel was designed to handle more than just integer/double values...and as I said, the list works fine - it's just the problem with attempting to assign a specific value to it where the issue occurs. – amthomas2112 May 05 '14 at 20:06
0

The answer to my problem was pretty much a derp on my part. I didn't properly initialize the default value I wanted the character attributes to have. Once I fixed that, the error no longer occurred, and the values in the spinners were assigned and displayed correctly.

For those who might be newer to Java and/or GUI design...

Right before the initialization of the spinner that was throwing the exception, I added a System.out.println(); for the two values in question:

agiSpin.setModel(new SpinnerListModel(DieType.values()));
//Check to see what value was being passed from the swchar Object
System.out.println(swchar.getAgiAttr()); 

//I wanted to see what the actual value was that was assigned to the Spinner when I initialized it with DieType.values()
System.out.println(agiSpin.getValue()); 
agiSpin.setValue(swchar.getAgiAttr());

I had thought I initialized the variable agiAttr as being DieType.D4, but the System.out.println() told me the truth: getAgiAttr() returned a value of null, and as such, agiSpin.getValue() was returning "d4", because that's the value it was being assigned by instantiating the spinner, not from the assignment I was attemping.

It was a noob error on my part, but hopefully my error will help others who might happen upon this question. Thank you to anyone who took the time to read/answer this.

amthomas2112
  • 47
  • 1
  • 1
  • 10