javaswingbuttonjspinner

Is it possible to disable a JSpinner down arrow button? How?


I have a JSpinner which starts at 0 and when its value is 0, its down arrow button should be disabled so the value does not change. Does anyone knows how to disable a button in a JSpinner?


Solution

  • You can use a SpinnerModel. E.g. the following line limits the values between 0 and MAX_VALUE (i.e. values are bounded by zero from below). Arguments are: value, min, max, step_size.

    JSpinner jSpinner = new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1));
    

    Note: the button is not disabled actually, but does not decrease the value.