How to set JSpinner as non editable?

后端 未结 3 1607
不思量自难忘°
不思量自难忘° 2020-12-07 00:26

I am creating time picker using a JSpinner. The text inside the JSpinner is editable. But I want to set the JSpinner as non editable,

相关标签:
3条回答
  • 2020-12-07 01:17

    Try the following:

    JSpinner spinner = ...;
    ((DefaultEditor) spinner.getEditor()).getTextField().setEditable(false);
    

    This should work as long as you didn't change the spinner editor yourself by calling spinner.setEditor(...).

    Tell us if this helps.

    0 讨论(0)
  • 2020-12-07 01:21

    When I try this, the spinner can still be edited by click the arrow! – yelliver

    You can try setting the step to 0:

    mySpinner.setModel(new SpinnerNumberModel(yourDefaultDisplayValue, minValue, maxValue, step));

    You can explore the other spinner models and do the same trick I guess.

    0 讨论(0)
  • 2020-12-07 01:23

    A bit shorter:

    JSpinner spinner = new JSpinner();
    spinner.setEditor(new JSpinner.DefaultEditor(spinner));
    
    0 讨论(0)
提交回复
热议问题